-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.cjs
82 lines (65 loc) · 2.21 KB
/
index.cjs
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
'use strict'
const pino = require('pino')
const flatstr = require('flatstr')
const createDefaultOptions = (env) => ({
createdAt: Date.now(),
level: env.LOG_LEVEL || 'info',
get timestamp () {
if (env.LOG_NODATE !== 'false') {
Object.defineProperty(this, 'timestamp', { value: false })
return false
}
if (env.LOG_DATE_FORMAT in pino.stdTimeFunctions) {
Object.defineProperty(this, 'timestamp', { value: pino.stdTimeFunctions[env.LOG_DATE_FORMAT] })
return pino.stdTimeFunctions[env.LOG_DATE_FORMAT]
}
Object.defineProperty(this, 'timestamp', { value: pino.stdTimeFunctions.epochTime })
return pino.stdTimeFunctions.epochTime
},
get makeItShort () {
return env.LOG_SHORT !== 'false'
},
formatters: {
level: label => ({ level: label })
},
base: null
})
const defaultOptions = createDefaultOptions(process.env)
// Default is this
// ',"pid":2365,"hostname":"daniel-XPS-15-7590"'
function setRequestId (RequestId, substrStart = -12, substrEnd) {
this.requestId = this.madeShort
? ('' + RequestId).substr(substrStart, substrEnd)
: RequestId
this[pino.symbols.chindingsSym] = flatstr(`,"Id":"${this.requestId}"`)
}
function makeItShort () {
this.madeShort = true
Object.keys(this[pino.symbols.lsCacheSym]).forEach((k) => {
const { level } = JSON.parse(`${this[pino.symbols.lsCacheSym][k]}}`)
this[pino.symbols.lsCacheSym][k] = (typeof level === 'number')
? flatstr(`{"l":"${this.levels.labels[level][0]}"`)
: flatstr(`{"l":"${level[0]}"`)
})
}
const createLogger = (options = {}) => {
const pinoOptions = { ...defaultOptions, ...options }
const mainLogr = Object.create(
pino(pinoOptions),
{
createLogger: { value: createLogger },
setRequestId: { value: setRequestId },
requestId: { value: null, writable: true },
makeItShort: { value: makeItShort },
// madeShort: { value: false, writable: true },
createdAt: { value: pinoOptions.createdAt, writable: true },
createDefaultOptions: { value: createDefaultOptions }
}
)
if (pinoOptions.makeItShort) {
mainLogr.makeItShort()
}
mainLogr[pino.symbols.endSym] = '}\n'
return mainLogr
}
module.exports = createLogger()