-
Notifications
You must be signed in to change notification settings - Fork 7
/
colors.js
101 lines (93 loc) · 2.44 KB
/
colors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
const chalk = require('colors')
const utils = require('./utils')
/**
* @type Array.<Function>
* @private
*
* Ranked list of status colors.
*/
const statusColors = [
chalk.green,
chalk.green,
chalk.green,
chalk.yellow,
chalk.red,
chalk.red
]
/**
* @type Array.<Function>
* @private
*
* Ranked list of level colors.
*/
const levelColors = [
chalk.grey,
chalk.blue,
chalk.green,
chalk.yellow,
chalk.red,
chalk.red
]
/**
* @function
* @public
*
* Get `chalk` builder function related to the status
* code of the response. If there is no response object,
* get a no-op function. If enable the `basic` mode,
* just get the index.
*
* @param {Object} data The data aggregated by `pino`
* @param {boolean} [basic=false] Request the basic index
* @returns {number | Function} The index or the related builder function
*/
function getStatusColor (data, basic = false) {
const code = data.res && data.res.statusCode
const range = parseInt(code / 100)
return basic ? range : statusColors[range] || utils.noop
}
/**
* @function
* @public
*
* Get `chalk` builder function related to the logging
* level. If there is a logging level out of range,
* get a no-op function. If enable the `basic` mode,
* just get the index.
*
* @param {Object} level The level extracted of the aggregated data
* @param {boolean} [basic=false] Request the basic index
* @returns {number | Function} The index or the related builder function
*/
function getLevelColor ({ level }, basic = false) {
const range = parseInt(level / 10 - 1)
return basic ? range : levelColors[range] || utils.noop
}
/**
* @function
* @public
*
* Get all `chalk` builder functions for colorizing
* console outputs based on the aggregated data. If
* get calculation gets skipped – in case of JSONs or
* for debugging needs – just get a map of no-ops.
* `status` is used for status code related colorization.
* `level` is used for logging level related colorization.
* `dim` is used for a dimmed colorization.
*
* @param {Object} data The data aggregated by `pino`
* @param {any} [skip=false] Skip the color calculation
* @returns {Object.<Function>} The map of colorization functions
*/
function get (data, skip = false) {
return {
status: skip ? utils.noop : getStatusColor(data),
level: skip ? utils.noop : getLevelColor(data),
dim: skip ? utils.noop : chalk.grey
}
}
module.exports = {
getStatus: getStatusColor,
getLevel: getLevelColor,
get
}