forked from pinojs/pino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpino.js
139 lines (126 loc) Β· 3.81 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
'use strict'
const os = require('os')
const stdSerializers = require('pino-std-serializers')
const redaction = require('./lib/redaction')
const time = require('./lib/time')
const proto = require('./lib/proto')
const symbols = require('./lib/symbols')
const { assertDefaultLevelFound, mappings, genLsCache } = require('./lib/levels')
const {
createArgsNormalizer,
asChindings,
final,
stringify,
buildSafeSonicBoom
} = require('./lib/tools')
const { version, LOG_VERSION } = require('./lib/meta')
const {
chindingsSym,
redactFmtSym,
serializersSym,
timeSym,
timeSliceIndexSym,
streamSym,
stringifySym,
stringifiersSym,
setLevelSym,
endSym,
formatOptsSym,
messageKeySym,
useLevelLabelsSym,
changeLevelNameSym,
mixinSym,
useOnlyCustomLevelsSym
} = symbols
const { epochTime, nullTime } = time
const { pid } = process
const hostname = os.hostname()
const defaultErrorSerializer = stdSerializers.err
const defaultOptions = {
level: 'info',
useLevelLabels: false,
messageKey: 'msg',
enabled: true,
prettyPrint: false,
base: { pid, hostname },
serializers: Object.assign(Object.create(null), {
err: defaultErrorSerializer
}),
timestamp: epochTime,
name: undefined,
redact: null,
customLevels: null,
changeLevelName: 'level',
useOnlyCustomLevels: false
}
const normalize = createArgsNormalizer(defaultOptions)
const serializers = Object.assign(Object.create(null), stdSerializers)
function pino (...args) {
const { opts, stream } = normalize(...args)
const {
redact,
crlf,
serializers,
timestamp,
messageKey,
base,
name,
level,
customLevels,
useLevelLabels,
changeLevelName,
mixin,
useOnlyCustomLevels
} = opts
const stringifiers = redact ? redaction(redact, stringify) : {}
const formatOpts = redact
? { stringify: stringifiers[redactFmtSym] }
: { stringify }
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 timeSliceIndex = time().indexOf(':') + 1
if (useOnlyCustomLevels && !customLevels) throw Error('customLevels is required if useOnlyCustomLevels is set true')
if (mixin && typeof mixin !== 'function') throw Error(`Unknown mixin type "${typeof mixin}" - expected "function"`)
assertDefaultLevelFound(level, customLevels, useOnlyCustomLevels)
const levels = mappings(customLevels, useOnlyCustomLevels)
const instance = {
levels,
[useLevelLabelsSym]: useLevelLabels,
[changeLevelNameSym]: changeLevelName,
[useOnlyCustomLevelsSym]: useOnlyCustomLevels,
[streamSym]: stream,
[timeSym]: time,
[timeSliceIndexSym]: timeSliceIndex,
[stringifySym]: stringify,
[stringifiersSym]: stringifiers,
[endSym]: end,
[formatOptsSym]: formatOpts,
[messageKeySym]: messageKey,
[serializersSym]: serializers,
[mixinSym]: mixin,
[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) => buildSafeSonicBoom(dest, 4096, false)
pino.destination = (dest = process.stdout.fd) => buildSafeSonicBoom(dest, 0, true)
pino.final = final
pino.levels = mappings()
pino.stdSerializers = serializers
pino.stdTimeFunctions = Object.assign({}, time)
pino.symbols = symbols
pino.version = version
pino.LOG_VERSION = LOG_VERSION
module.exports = pino