-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathlogger.js
122 lines (108 loc) · 3.63 KB
/
logger.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const escapeStringRegexp = require('escape-string-regexp')
const util = require('node:util')
const supportsColors = require('supports-color')
const valuesToMask = []
/**
* Adds a list of strings that should be masked by the logger.
* This function can only be called once through out the life of the server.
*
* @param {Array} maskables a list of strings to be masked
*/
exports.setMaskables = (maskables) => {
maskables.forEach((i) => {
valuesToMask.push(escapeStringRegexp(i))
})
Object.freeze(valuesToMask)
}
/**
* Mask the secret content of a message
*
* @param {string} msg the message whose content should be masked
* @returns {string}
*/
function maskMessage(msg) {
let out = msg
for (const toBeMasked of valuesToMask) {
const toBeReplaced = new RegExp(toBeMasked, 'gi')
out = out.replace(toBeReplaced, '******')
}
return out
}
let processName = 'companion'
exports.setProcessName = (newProcessName) => {
processName = newProcessName
}
const styleText =
typeof util.styleText === "function" && supportsColors.stderr ?
util.styleText
: (style, text) => text;
/**
* message log
*
* @param {object} params
* @param {string | Error} params.arg the message or error to log
* @param {string} params.tag a unique tag to easily search for this message
* @param {string} params.level error | info | debug
* @param {string} [params.traceId] a unique id to easily trace logs tied to a request
* @param {string[]} [params.color] Format(s) that can be passed to `util.styleText`.
*/
const log = ({ arg, tag = '', level, traceId = '', color = [] }) => {
const time = new Date().toISOString()
const whitespace = tag && traceId ? ' ' : ''
function msgToString() {
// We don't need to log stack trace on special errors that we ourselves have produced
// (to reduce log noise)
// @ts-ignore
if ((arg instanceof Error && arg.name === 'ProviderApiError') && typeof arg.message === 'string') {
return arg.message
}
if (typeof arg === 'string') return arg
return util.inspect(arg)
}
const msgString = msgToString()
const masked = maskMessage(msgString)
// eslint-disable-next-line no-console
console.log(styleText(color, `${processName}: ${time} [${level}] ${traceId}${whitespace}${tag}`), styleText(color, masked))
}
/**
* INFO level log
*
* @param {string} msg the message to log
* @param {string} [tag] a unique tag to easily search for this message
* @param {string} [traceId] a unique id to easily trace logs tied to a request
*/
exports.info = (msg, tag, traceId) => {
log({ arg: msg, tag, level: 'info', traceId })
}
/**
* WARN level log
*
* @param {string} msg the message to log
* @param {string} [tag] a unique tag to easily search for this message
* @param {string} [traceId] a unique id to easily trace logs tied to a request
*/
exports.warn = (msg, tag, traceId) => {
log({ arg: msg, tag, level: 'warn', traceId, color: ['bold', 'yellow'] })
}
/**
* ERROR level log
*
* @param {string | Error} msg the message to log
* @param {string} [tag] a unique tag to easily search for this message
* @param {string} [traceId] a unique id to easily trace logs tied to a request
*/
exports.error = (msg, tag, traceId) => {
log({ arg: msg, tag, level: 'error', traceId, color: ['bold', 'red'] })
}
/**
* DEBUG level log
*
* @param {string} msg the message to log
* @param {string} [tag] a unique tag to easily search for this message
* @param {string} [traceId] a unique id to easily trace logs tied to a request
*/
exports.debug = (msg, tag, traceId) => {
if (process.env.NODE_ENV !== 'production') {
log({ arg: msg, tag, level: 'debug', traceId, color: ['bold', 'blue'] })
}
}