forked from pinojs/pino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pino.js
123 lines (112 loc) · 3.22 KB
/
pino.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
'use strict'
const os = require('os')
const serializers = require('pino-std-serializers')
const SonicBoom = require('sonic-boom')
const redaction = require('./lib/redaction')
const time = require('./lib/time')
const proto = require('./lib/proto')
const symbols = require('./lib/symbols')
const { mappings, genLsCache, assertNoLevelCollisions } = require('./lib/levels')
const {
createArgsNormalizer,
asChindings,
final,
stringify
} = require('./lib/tools')
const { version, LOG_VERSION } = require('./lib/meta')
const {
chindingsSym,
redactFmtSym,
serializersSym,
timeSym,
streamSym,
stringifySym,
stringifiersSym,
setLevelSym,
endSym,
formatOptsSym,
messageKeyStringSym,
useLevelLabelsSym,
changeLevelNameSym
} = symbols
const { epochTime, nullTime } = time
const { pid } = process
const hostname = os.hostname()
const defaultErrorSerializer = serializers.err
const defaultOptions = {
level: 'info',
useLevelLabels: false,
messageKey: 'msg',
enabled: true,
prettyPrint: false,
base: { pid, hostname },
serializers: {err: defaultErrorSerializer},
timestamp: epochTime,
name: undefined,
redact: null,
customLevels: null,
changeLevelName: 'level'
}
const normalize = createArgsNormalizer(defaultOptions)
function pino (...args) {
const { opts, stream } = normalize(...args)
const {
redact,
crlf,
serializers,
timestamp,
messageKey,
base,
name,
level,
customLevels,
useLevelLabels,
changeLevelName
} = opts
assertNoLevelCollisions(pino.levels, customLevels)
const stringifiers = redact ? redaction(redact, stringify) : {}
const formatOpts = redact
? {stringify: stringifiers[redactFmtSym]}
: { stringify }
const messageKeyString = `,"${messageKey}":`
const end = ',"v":' + LOG_VERSION + '}' + (crlf ? '\r\n' : '\n')
const coreChindings = asChindings.bind(null, {
[chindingsSym]: '',
[serializersSym]: serializers,
[stringifiersSym]: stringifiers,
[stringifySym]: stringify
})
const chindings = base === null ? '' : (name === undefined)
? coreChindings(base) : coreChindings(Object.assign({}, base, { name }))
const time = (timestamp instanceof Function)
? timestamp : (timestamp ? epochTime : nullTime)
const levels = mappings(customLevels)
const instance = {
levels,
[useLevelLabelsSym]: useLevelLabels,
[changeLevelNameSym]: changeLevelName,
[streamSym]: stream,
[timeSym]: time,
[stringifySym]: stringify,
[stringifiersSym]: stringifiers,
[endSym]: end,
[formatOptsSym]: formatOpts,
[messageKeyStringSym]: messageKeyString,
[serializersSym]: serializers,
[chindingsSym]: chindings
}
Object.setPrototypeOf(instance, proto)
if (customLevels || useLevelLabels || changeLevelName !== defaultOptions.changeLevelName) genLsCache(instance)
instance[setLevelSym](level)
return instance
}
pino.extreme = (dest = process.stdout.fd) => new SonicBoom(dest, 4096)
pino.destination = (dest = process.stdout.fd) => new SonicBoom(dest)
pino.final = final
pino.levels = mappings()
pino.stdSerializers = Object.assign({}, serializers)
pino.stdTimeFunctions = Object.assign({}, time)
pino.symbols = symbols
pino.version = version
pino.LOG_VERSION = LOG_VERSION
module.exports = pino