-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathnode.js
More file actions
415 lines (385 loc) · 10.3 KB
/
node.js
File metadata and controls
415 lines (385 loc) · 10.3 KB
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
import os from 'os'
import ms from 'ms'
import chalk from 'chalk'
import fastStringify from 'fast-safe-stringify'
import flatstr from 'flatstr'
import {
fromNumLevel,
inspectOpts,
saveOpts,
inspectNamespaces,
selectColor,
levelColors,
INFO
} from './utils.js'
import { LogBase } from './LogBase.js'
import { wrapConsole } from './wrapConsole.js'
import { wrapDebug } from './wrapDebug.js'
import { Sonic, sonicStreams } from './Sonic.js'
import { errSerializer } from './serializers/index.js'
import { Format } from './Format.js'
const env = process.env.NODE_ENV || 'development'
const isDevEnv = /^dev/.test(env) // anything which starts with dev is seen as development env
const EXIT_EVENTS = ['unhandledRejection', 'uncaughtException']
/** @typedef {import('./LogBase.js').LogBaseOptions} LogBaseOptions */
/** @typedef {import('./utils.js').Level} Level */
/**
* @typedef {object} ExtLogOptions
* @property {boolean} [serverinfo] log serverinfo like hostname and pid
* @property {NodeJS.WriteStream} [stream=process.stderr] stream writer
* @property {boolean} [sonic] use sonic (default for production use)
* @property {number} [sonicLength=4096] buffer length for Sonic
* @property {number} [sonicFlushMs=1000] min. timeout before flush of Sonic buffer in ms
*/
/**
* @typedef {LogBaseOptions & ExtLogOptions} LogOptions
*/
/**
* @typedef {object} ExtLogOptionWrapConsole
* @property {Level} [level4log='LOG']
*/
/**
* @typedef {LogOptions & ExtLogOptionWrapConsole} LogOptionWrapConsole
*/
/**
* @typedef {object} ExtLogOptionHandleExitEvents
* @param {boolean} [code=1] set exit code; code=0 will prevent triggering exit
* @param {boolean} [gracefulExit=false] uses process.exitCode to avoid forceful exit with process.exit()
*/
/**
* @typedef {LogOptions & ExtLogOptionHandleExitEvents} LogOptionHandleExitEvents
*/
/**
* global log options
* @type {LogOptions}
*/
const options = {
level: INFO,
namespaces: undefined,
levelNumbers: false,
json: !isDevEnv, // log in json format
colors: isDevEnv, // apply colors
timestamp: !isDevEnv ? 'iso' : undefined, // time format: either `epoch`, `unix`, `iso`
serverinfo: !isDevEnv, // append server information
stream: process.stderr, // output stream
sonic: !isDevEnv, // use Sonic as stream writer
sonicLength: 4096, // buffer length for Sonic
sonicFlushMs: 1000, // min. timeout before write
spaces: undefined, // pretty print JSON
splitLine: isDevEnv, // split lines for pretty debug like output
serializers: { err: errSerializer } // serializers definition
}
export class Log extends LogBase {
/**
* creates a new logger
* @param {String} name - namespace of Logger
* @param {LogOptions} [opts] - see Log.options
*/
constructor(name, opts) {
Object.assign(
options,
inspectOpts(process.env),
inspectNamespaces(process.env)
)
const serializers = Object.assign(
{},
options.serializers,
opts ? opts.serializers : {}
)
const _opts = { ...options, ...opts, serializers }
super(name, _opts)
const colorFn = (n) => chalk.hex(n)
this.color = selectColor(name, colorFn)
this.levColors = levelColors(colorFn)
// noop for TS
this.opts = {
stream: process.stderr,
..._opts,
...this.opts
}
this.toJson = toJson
this.stream = this.opts.sonic
? sonicStreams.use(this.opts.stream, {
minLength: this.opts.sonicLength,
timeout: this.opts.sonicFlushMs
})
: this.opts.stream || process.stderr
if (!this.opts.json) {
this.opts.spaces = this.opts.spaces ?? 2
this.formatter = new Format(this.opts)
this._log = this._logDebugLike
} else if (this.opts.colors) {
this._log = this._logJsonColor
}
if (this.opts.serverinfo) {
this.hostname = os.hostname()
this.pid = process.pid
}
}
/**
* Apply (and get) global options
* @param {object} [opts] changed options
* @return {object} global options
*/
static options(opts) {
if (!opts) {
return { ...options }
}
return Object.assign(options, opts)
}
/**
* save options in `process.env`
*/
static save() {
Log.reset()
saveOpts(process.env, options)
}
/**
* reset saved options
*/
static reset() {
Object.keys(process.env).forEach((key) => {
if (/^(DEBUG|DEBUG_.*)$/.test(key)) {
delete process.env[key]
}
})
}
/**
* wrap console logging functions like
* console.log, console.info, console.warn, console.error
* @param {string} [name='console']
* @param {LogOptionWrapConsole} [opts] options
* @return {function} unwrap function
*/
static wrapConsole(name = 'console', opts) {
const log = new Log(name, opts)
return wrapConsole(log, opts)
}
/**
* log exit events like 'unhandledRejection', 'uncaughtException'
* and then let the process die
* @param {string} [name='exit']
* @param {LogOptionHandleExitEvents} [opts] options
*/
static handleExitEvents(name = 'exit', opts = {}) {
const { code = 1, gracefulExit, ..._opts } = opts
const log = new Log(name, _opts)
EXIT_EVENTS.forEach((ev) => {
process.on(ev, (err) => {
log.fatal(err)
/* c8 ignore next 7 */
if (code) {
if (gracefulExit) {
process.exitCode = code // let die...
} else {
setTimeout(() => process.exit(code), 5)
}
}
})
})
}
/**
* @param {LogOptions} [opts] - see Log.options
* @returns {() => void} unwrap function
*/
static wrapDebug(opts) {
return wrapDebug(Log, opts)
}
/**
* render string to output stream
* @public
* @param {String} str string to render
* @param {String} [_level] level of log line (might be used for custom Logger which uses different streams per level)
* @return {String}
*/
render(str, _level) {
this.stream.write(flatstr(str + '\n'))
return str
}
flush() {
// @ts-expect-error
this.stream.flush && this.stream.flush()
}
/**
* format object to json
* @protected
*/
_log(level, fmt, args) {
const o = this._formatJson(level, fmt, args)
const str = this.toJson(o, this.serializers)
return this.render(str, level)
}
/**
* format object to json
* @private
*/
_logJsonColor(level, fmt, args) {
const o = this._formatJson(level, fmt, args)
let str = this.toJson(o, this.serializers)
/* c8 ignore next 4 */ // can't cover with tests as underlying tty is unknown
str = str
.replace(/"level":\s?"([^"]+)"/, (m, level) =>
this._color(m, this.levColors[level], true)
)
.replace(/"level":\s?(\d+)/, (m, level) =>
this._color(m, this.levColors[fromNumLevel(Number(level))], true)
)
.replace(/"name":\s?"[^"]+"/, (m) => this._color(m, this.color, true))
return this.render(str, level)
}
/**
* debug like output if `this.opts.json === false`
* @private
*/
_logDebugLike(_level, fmt, args) {
const o = this._formatJson(_level, fmt, args)
/* eslint-disable no-unused-vars */
const { level, time, name, msg, pid, hostname, diff, ...other } = o
/* eslint-enable no-unused-vars */
const prefix =
' ' +
this._color(level, this.levColors[level], true) +
' ' +
this._color(this.name, this.color, true)
const strOther = Object.keys(other).length
? stringify(
this._applySerializers(other),
this.opts.splitLine ? (this.opts.spaces ?? 2) : undefined
)
: ''
const str = this.opts.splitLine
? [
prefix,
time,
msg,
strOther,
this._color('+' + ms(diff), this.color),
hostname,
pid
]
.filter((s) => s)
.join(' ')
.split(/\\n|\n/)
.join('\n' + prefix + ' ')
: [
prefix,
time,
replaceLf(msg),
strOther,
this._color('+' + ms(diff), this.color),
hostname,
pid
]
.filter((s) => s)
.join(' ')
return this.render(str, level)
}
/**
* Add colors, style to string
* @private
*/
_color(str, color, isBold) {
return !color || !this.opts?.colors
? str
: isBold
? color.bold(str)
: color(str)
}
}
Log.isDevEnv = isDevEnv
Log.Sonic = Sonic
Log.serializers = {
err: errSerializer
}
/**
* @credits pino/lib/tools.js
*
* magically escape strings for json relying on charCodeAt;
* everything below 32 needs JSON.stringify() 34 and 92 happens all the time, so
* we have a fast case for them
*
* @param {string} str
* @returns {string}
*/
function asString(str) {
let result = ''
let last = 0
let found = false
let point = 255
const l = str.length
if (l > 100) {
return JSON.stringify(str)
}
for (let i = 0; i < l && point >= 32; i++) {
point = str.charCodeAt(i)
if (point === 34 || point === 92) {
result += str.slice(last, i) + '\\'
last = i
found = true
}
}
if (!found) {
result = str
} else {
result += str.slice(last)
}
return point < 32 ? JSON.stringify(str) : '"' + result + '"'
}
/**
* @param {object} obj
* @param {object} serializers
* @param {number} [spaces]
* @returns {string}
*/
function toJson(obj, serializers, spaces) {
let s = '{'
let comma = ''
for (const key in obj) {
let value = obj[key]
if (Object.prototype.hasOwnProperty.call(obj, key) && value !== undefined) {
if (serializers && serializers[key]) {
value = serializers[key](value, obj)
}
switch (typeof value) {
case 'function':
continue
case 'number':
if (!Number.isFinite(value)) {
continue
}
break
case 'boolean':
break
case 'string':
value = asString(value)
break
default:
value = stringify(value, spaces)
break
}
s += comma + asString(key) + ':' + value
comma = ','
}
}
return s + '}'
}
/**
* @param {any} any
* @param {number} [spaces]
* @returns {string}
*/
export function stringify(any, spaces) {
try {
return JSON.stringify(any, null, spaces)
} catch (e) {
// @ts-expect-error
return fastStringify(any, null, spaces)
}
}
/**
* @param {string} str
* @returns {string}
*/
function replaceLf(str = '') {
return str.replace(/[\r\n]/g, '\\n')
}