-
Notifications
You must be signed in to change notification settings - Fork 151
/
utils.js
437 lines (394 loc) · 15 KB
/
utils.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
'use strict'
const dateformat = require('dateformat')
const stringifySafe = require('fast-safe-stringify')
const defaultColorizer = require('./colors')()
const {
DATE_FORMAT,
ERROR_LIKE_KEYS,
MESSAGE_KEY,
LEVEL_KEY,
LEVEL_LABEL,
TIMESTAMP_KEY,
LOGGER_KEYS,
LEVELS
} = require('./constants')
module.exports = {
isObject,
prettifyErrorLog,
prettifyLevel,
prettifyMessage,
prettifyMetadata,
prettifyObject,
prettifyTime
}
module.exports.internals = {
formatTime,
joinLinesWithIndentation,
prettifyError
}
/**
* Converts a given `epoch` to a desired display format.
*
* @param {number|string} epoch The time to convert. May be any value that is
* valid for `new Date()`.
* @param {boolean|string} [translateTime=false] When `false`, the given `epoch`
* will simply be returned. When `true`, the given `epoch` will be converted
* to a string at UTC using the `DATE_FORMAT` constant. If `translateTime` is
* a string, the following rules are available:
*
* - `<format string>`: The string is a literal format string. This format
* string will be used to interpret the `epoch` and return a display string
* at UTC.
* - `SYS:STANDARD`: The returned display string will follow the `DATE_FORMAT`
* constant at the system's local timezone.
* - `SYS:<format string>`: The returned display string will follow the given
* `<format string>` at the system's local timezone.
* - `UTC:<format string>`: The returned display string will follow the given
* `<format string>` at UTC.
*
* @returns {number|string} The formatted time.
*/
function formatTime (epoch, translateTime = false) {
if (translateTime === false) {
return epoch
}
const instant = new Date(epoch)
if (translateTime === true) {
return dateformat(instant, 'UTC:' + DATE_FORMAT)
}
const upperFormat = translateTime.toUpperCase()
if (upperFormat === 'SYS:STANDARD') {
return dateformat(instant, DATE_FORMAT)
}
const prefix = upperFormat.substr(0, 4)
if (prefix === 'SYS:' || prefix === 'UTC:') {
if (prefix === 'UTC:') {
return dateformat(instant, translateTime)
}
return dateformat(instant, translateTime.slice(4))
}
return dateformat(instant, `UTC:${translateTime}`)
}
function isObject (input) {
return Object.prototype.toString.apply(input) === '[object Object]'
}
/**
* Given a string with line separators, either `\r\n` or `\n`, add indentation
* to all lines subsequent to the first line and rejoin the lines using an
* end of line sequence.
*
* @param {object} input
* @param {string} input.input The string to split and reformat.
* @param {string} [input.ident] The indentation string. Default: ` ` (4 spaces).
* @param {string} [input.eol] The end of line sequence to use when rejoining
* the lines. Default: `'\n'`.
*
* @returns {string} A string with lines subsequent to the first indented
* with the given indentation sequence.
*/
function joinLinesWithIndentation ({ input, ident = ' ', eol = '\n' }) {
const lines = input.split(/\r?\n/)
for (let i = 1; i < lines.length; i += 1) {
lines[i] = ident + lines[i]
}
return lines.join(eol)
}
/**
* Given a log object that has a `type: 'Error'` key, prettify the object and
* return the result. In other
*
* @param {object} input
* @param {object} input.log The error log to prettify.
* @param {string} [input.messageKey] The name of the key that contains a
* general log message. This is not the error's message property but the logger
* messsage property. Default: `MESSAGE_KEY` constant.
* @param {string} [input.ident] The sequence to use for indentation. Default: `' '`.
* @param {string} [input.eol] The sequence to use for EOL. Default: `'\n'`.
* @param {string[]} [input.errorLikeKeys] A set of keys that should be considered
* to have error objects as values. Default: `ERROR_LIKE_KEYS` constant.
* @param {string[]} [input.errorProperties] A set of specific error object
* properties, that are not the value of `messageKey`, `type`, or `stack`, to
* include in the prettified result. The first entry in the list may be `'*'`
* to indicate that all sibiling properties should be prettified. Default: `[]`.
*
* @returns {string} A sring that represents the prettified error log.
*/
function prettifyErrorLog ({
log,
messageKey = MESSAGE_KEY,
ident = ' ',
eol = '\n',
errorLikeKeys = ERROR_LIKE_KEYS,
errorProperties = []
}) {
const stack = log.stack
const joinedLines = joinLinesWithIndentation({ input: stack, ident, eol })
let result = `${ident}${joinedLines}${eol}`
if (errorProperties.length > 0) {
const excludeProperties = LOGGER_KEYS.concat(messageKey, 'type', 'stack')
let propertiesToPrint
if (errorProperties[0] === '*') {
// Print all sibling properties except for the standard exclusions.
propertiesToPrint = Object.keys(log).filter(k => excludeProperties.includes(k) === false)
} else {
// Print only sepcified properties unless the property is a standard exclusion.
propertiesToPrint = errorProperties.filter(k => excludeProperties.includes(k) === false)
}
for (let i = 0; i < propertiesToPrint.length; i += 1) {
const key = propertiesToPrint[i]
if (key in log === false) continue
if (isObject(log[key])) {
// The nested object may have "logger" type keys but since they are not
// at the root level of the object being processed, we want to print them.
// Thus, we invoke with `excludeLoggerKeys: false`.
const prettifiedObject = prettifyObject({ input: log[key], errorLikeKeys, excludeLoggerKeys: false, eol, ident })
result = `${result}${key}: {${eol}${prettifiedObject}}${eol}`
continue
}
result = `${result}${key}: ${log[key]}${eol}`
}
}
return result
}
/**
* Checks if the passed in log has a `level` value and returns a prettified
* string for that level if so.
*
* @param {object} input
* @param {object} input.log The log object.
* @param {function} [input.colorizer] A colorizer function that accepts a level
* value and returns a colorized string. Default: a no-op colorizer.
* @param {string} [levelKey='level'] The key to find the level under.
*
* @returns {undefined|string} If `log` does not have a `level` property then
* `undefined` will be returned. Otherwise, a string from the specified
* `colorizer` is returned.
*/
function prettifyLevel ({ log, colorizer = defaultColorizer, levelKey = LEVEL_KEY }) {
if (levelKey in log === false) return undefined
return colorizer(log[levelKey])
}
/**
* Prettifies a message string if the given `log` has a message property.
*
* @param {object} input
* @param {object} input.log The log object with the message to colorize.
* @param {string} [input.messageKey='msg'] The property of the `log` that is the
* message to be prettified.
* @param {string|function} [input.messageFormat=undefined] A format string or function that defines how the
* logged message should be formatted, e.g. `'{level} - {pid}'`.
* @param {function} [input.colorizer] A colorizer function that has a
* `.message(str)` method attached to it. This function should return a colorized
* string which will be the "prettified" message. Default: a no-op colorizer.
*
* @returns {undefined|string} If the message key is not found, or the message
* key is not a string, then `undefined` will be returned. Otherwise, a string
* that is the prettified message.
*/
function prettifyMessage ({ log, messageFormat, messageKey = MESSAGE_KEY, colorizer = defaultColorizer, levelLabel = LEVEL_LABEL }) {
if (messageFormat && typeof messageFormat === 'string') {
const message = String(messageFormat).replace(/{([^{}]+)}/g, function (match, p1) {
// return log level as string instead of int
if (p1 === levelLabel && log[LEVEL_KEY]) {
return LEVELS[log[LEVEL_KEY]]
}
// Parse nested key access, e.g. `{keyA.subKeyB}`.
return p1.split('.').reduce(function (prev, curr) {
if (prev && prev[curr]) {
return prev[curr]
}
return ''
}, log)
})
return colorizer.message(message)
}
if (messageFormat && typeof messageFormat === 'function') {
const msg = messageFormat(log, messageKey, levelLabel)
return colorizer.message(msg)
}
if (messageKey in log === false) return undefined
if (typeof log[messageKey] !== 'string') return undefined
return colorizer.message(log[messageKey])
}
/**
* Prettifies metadata that is usually present in a Pino log line. It looks for
* fields `name`, `pid`, `hostname`, and `caller` and returns a formatted string using
* the fields it finds.
*
* @param {object} input
* @param {object} input.log The log that may or may not contain metadata to
* be prettified.
*
* @returns {undefined|string} If no metadata is found then `undefined` is
* returned. Otherwise, a string of prettified metadata is returned.
*/
function prettifyMetadata ({ log }) {
let line = ''
if (log.name || log.pid || log.hostname) {
line += '('
if (log.name) {
line += log.name
}
if (log.name && log.pid) {
line += '/' + log.pid
} else if (log.pid) {
line += log.pid
}
if (log.hostname) {
// If `pid` and `name` were in the ignore keys list then we don't need
// the leading space.
line += `${line === '(' ? 'on' : ' on'} ${log.hostname}`
}
line += ')'
}
if (log.caller) {
line += `${line === '' ? '' : ' '}<${log.caller}>`
}
if (line === '') {
return undefined
} else {
return line
}
}
/**
* Prettifies a standard object. Special care is taken when processing the object
* to handle child objects that are attached to keys known to contain error
* objects.
*
* @param {object} input
* @param {object} input.input The object to prettify.
* @param {string} [input.ident] The identation sequence to use. Default: `' '`.
* @param {string} [input.eol] The EOL sequence to use. Default: `'\n'`.
* @param {string[]} [input.skipKeys] A set of object keys to exclude from the
* prettified result. Default: `[]`.
* @param {Object<string, function>} [input.customPrettifiers] Dictionary of
* custom prettifiers. Default: `{}`.
* @param {string[]} [input.errorLikeKeys] A set of object keys that contain
* error objects. Default: `ERROR_LIKE_KEYS` constant.
* @param {boolean} [input.excludeLoggerKeys] Indicates if known logger specific
* keys should be excluded from prettification. Default: `true`.
* @param {boolean} [input.singleLine] Should non-error keys all be formatted
* on a single line? This does NOT apply to errors, which will still be
* multi-line. Default: `false`
*
* @returns {string} The prettified string. This can be as little as `''` if
* there was nothing to prettify.
*/
function prettifyObject ({
input,
ident = ' ',
eol = '\n',
skipKeys = [],
customPrettifiers = {},
errorLikeKeys = ERROR_LIKE_KEYS,
excludeLoggerKeys = true,
singleLine = false,
colorizer = defaultColorizer
}) {
const keysToIgnore = [].concat(skipKeys)
if (excludeLoggerKeys === true) Array.prototype.push.apply(keysToIgnore, LOGGER_KEYS)
let result = ''
// Split object keys into two categories: error and non-error
const { plain, errors } = Object.entries(input).reduce(({ plain, errors }, [k, v]) => {
if (keysToIgnore.includes(k) === false) {
// Pre-apply custom prettifiers, because all 3 cases below will need this
const pretty = typeof customPrettifiers[k] === 'function'
? customPrettifiers[k](v, k, input)
: v
if (errorLikeKeys.includes(k)) {
errors[k] = pretty
} else {
plain[k] = pretty
}
}
return { plain, errors }
}, { plain: {}, errors: {} })
if (singleLine) {
// Stringify the entire object as a single JSON line
if (Object.keys(plain).length > 0) {
result += colorizer.greyMessage(stringifySafe(plain))
}
result += eol
} else {
// Put each object entry on its own line
Object.entries(plain).forEach(([keyName, keyValue]) => {
// custom prettifiers are already applied above, so we can skip it now
const lines = typeof customPrettifiers[keyName] === 'function'
? keyValue
: stringifySafe(keyValue, null, 2)
if (lines === undefined) return
const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol })
result += `${ident}${keyName}: ${joinedLines}${eol}`
})
}
// Errors
Object.entries(errors).forEach(([keyName, keyValue]) => {
// custom prettifiers are already applied above, so we can skip it now
const lines = typeof customPrettifiers[keyName] === 'function'
? keyValue
: stringifySafe(keyValue, null, 2)
if (lines === undefined) return
result += prettifyError({ keyName, lines, eol, ident })
})
return result
}
/**
* Prettifies a timestamp if the given `log` has either `time`, `timestamp` or custom specified timestamp
* property.
*
* @param {object} input
* @param {object} input.log The log object with the timestamp to be prettified.
* @param {string} [input.timestampKey='time'] The log property that should be used to resolve timestamp value
* @param {boolean|string} [input.translateFormat=undefined] When `true` the
* timestamp will be prettified into a string at UTC using the default
* `DATE_FORMAT`. If a string, then `translateFormat` will be used as the format
* string to determine the output; see the `formatTime` function for details.
*
* @returns {undefined|string} If a timestamp property cannot be found then
* `undefined` is returned. Otherwise, the prettified time is returned as a
* string.
*/
function prettifyTime ({ log, timestampKey = TIMESTAMP_KEY, translateFormat = undefined }) {
let time = null
if (timestampKey in log) {
time = log[timestampKey]
} else if ('timestamp' in log) {
time = log.timestamp
}
if (time === null) return undefined
if (translateFormat) {
return '[' + formatTime(time, translateFormat) + ']'
}
return `[${time}]`
}
/**
* Prettifies an error string into a multi-line format.
* @param {object} input
* @param {string} input.keyName The key assigned to this error in the log object
* @param {string} input.lines The STRINGIFIED error. If the error field has a
* custom prettifier, that should be pre-applied as well
* @param {string} input.ident The indentation sequence to use
* @param {string} input.eol The EOL sequence to use
*/
function prettifyError ({ keyName, lines, eol, ident }) {
let result = ''
const joinedLines = joinLinesWithIndentation({ input: lines, ident, eol })
const splitLines = `${ident}${keyName}: ${joinedLines}${eol}`.split(eol)
for (let j = 0; j < splitLines.length; j += 1) {
if (j !== 0) result += eol
const line = splitLines[j]
if (/^\s*"stack"/.test(line)) {
const matches = /^(\s*"stack":)\s*(".*"),?$/.exec(line)
/* istanbul ignore else */
if (matches && matches.length === 3) {
const indentSize = /^\s*/.exec(line)[0].length + 4
const indentation = ' '.repeat(indentSize)
const stackMessage = matches[2]
result += matches[1] + eol + indentation + JSON.parse(stackMessage).replace(/\n/g, eol + indentation)
}
} else {
result += line
}
}
return result
}