forked from pinojs/pino-pretty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
148 lines (128 loc) · 3.62 KB
/
index.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
'use strict'
const chalk = require('chalk')
const jmespath = require('jmespath')
const colors = require('./lib/colors')
const { ERROR_LIKE_KEYS, MESSAGE_KEY, TIMESTAMP_KEY } = require('./lib/constants')
const {
isObject,
prettifyErrorLog,
prettifyLevel,
prettifyMessage,
prettifyMetadata,
prettifyObject,
prettifyTime
} = require('./lib/utils')
const bourne = require('@hapi/bourne')
const jsonParser = input => {
try {
return { value: bourne.parse(input, { protoAction: 'remove' }) }
} catch (err) {
return { err }
}
}
const defaultOptions = {
colorize: chalk.supportsColor,
crlf: false,
errorLikeObjectKeys: ERROR_LIKE_KEYS,
errorProps: '',
levelFirst: false,
messageKey: MESSAGE_KEY,
timestampKey: TIMESTAMP_KEY,
translateTime: false,
useMetadata: false,
outputStream: process.stdout
}
module.exports = function prettyFactory (options) {
const opts = Object.assign({}, defaultOptions, options)
const EOL = opts.crlf ? '\r\n' : '\n'
const IDENT = ' '
const messageKey = opts.messageKey
const timestampKey = opts.timestampKey
const errorLikeObjectKeys = opts.errorLikeObjectKeys
const errorProps = opts.errorProps.split(',')
const ignoreKeys = opts.ignore ? new Set(opts.ignore.split(',')) : undefined
const colorizer = colors(opts.colorize)
const search = opts.search
return pretty
function pretty (inputData) {
let log
if (!isObject(inputData)) {
const parsed = jsonParser(inputData)
log = parsed.value
if (parsed.err) {
// pass through
return inputData + EOL
}
} else {
log = inputData
}
// Short-circuit for spec allowed primitive values.
if ([null, true, false].includes(log) || Number.isFinite(log)) {
return `${log}\n`
}
if (search && !jmespath.search(log, search)) {
return
}
if (ignoreKeys) {
log = Object.keys(log)
.filter(key => !ignoreKeys.has(key))
.reduce((res, key) => {
res[key] = log[key]
return res
}, {})
}
const prettifiedLevel = prettifyLevel({ log, colorizer })
const prettifiedMessage = prettifyMessage({ log, messageKey, colorizer })
const prettifiedMetadata = prettifyMetadata({ log })
const prettifiedTime = prettifyTime({ log, translateFormat: opts.translateTime, timestampKey })
let line = ''
if (opts.levelFirst && prettifiedLevel) {
line = `${prettifiedLevel}`
}
if (prettifiedTime && line === '') {
line = `${prettifiedTime}`
} else if (prettifiedTime) {
line = `${line} ${prettifiedTime}`
}
if (!opts.levelFirst && prettifiedLevel) {
if (line.length > 0) {
line = `${line} ${prettifiedLevel}`
} else {
line = prettifiedLevel
}
}
if (prettifiedMetadata) {
line = `${line} ${prettifiedMetadata}:`
}
if (line.endsWith(':') === false && line !== '') {
line += ':'
}
if (prettifiedMessage) {
line = `${line} ${prettifiedMessage}`
}
if (line.length > 0) {
line += EOL
}
if (log.type === 'Error' && log.stack) {
const prettifiedErrorLog = prettifyErrorLog({
log,
errorLikeKeys: errorLikeObjectKeys,
errorProperties: errorProps,
ident: IDENT,
eol: EOL
})
line += prettifiedErrorLog
} else {
const skipKeys = typeof log[messageKey] === 'string' ? [messageKey] : undefined
const prettifiedObject = prettifyObject({
input: log,
skipKeys,
errorLikeKeys: errorLikeObjectKeys,
eol: EOL,
ident: IDENT
})
line += prettifiedObject
}
return line
}
}