/
proto.js
144 lines (132 loc) · 3.56 KB
/
proto.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
'use strict'
const { EventEmitter } = require('events')
const SonicBoom = require('sonic-boom')
const flatstr = require('flatstr')
const {
lsCacheSym,
levelValSym,
setLevelSym,
getLevelSym,
chindingsSym,
asJsonSym,
messageKeySym,
writeSym,
timeSym,
timeSliceIndexSym,
streamSym,
serializersSym,
useOnlyCustomLevelsSym,
needsMetadataGsym
} = require('./symbols')
const {
getLevel,
setLevel,
isLevelEnabled,
mappings,
initialLsCache,
genLsCache,
assertNoLevelCollisions
} = require('./levels')
const {
asChindings,
asJson
} = require('./tools')
const {
version,
LOG_VERSION
} = require('./meta')
// note: use of class is satirical
// https://github.com/pinojs/pino/pull/433#pullrequestreview-127703127
const constructor = class Pino {}
const prototype = {
constructor,
child,
bindings,
flush,
isLevelEnabled,
version,
get level () { return this[getLevelSym]() },
set level (lvl) { return this[setLevelSym](lvl) },
get levelVal () { return this[levelValSym] },
set levelVal (n) { throw Error('levelVal is read-only') },
[lsCacheSym]: initialLsCache,
[writeSym]: write,
[asJsonSym]: asJson,
[getLevelSym]: getLevel,
[setLevelSym]: setLevel,
LOG_VERSION
}
Object.setPrototypeOf(prototype, EventEmitter.prototype)
module.exports = prototype
function child (bindings) {
const { level } = this
const serializers = this[serializersSym]
const chindings = asChindings(this, bindings)
const instance = Object.create(this)
if (bindings.hasOwnProperty('serializers') === true) {
instance[serializersSym] = Object.create(null)
for (var k in serializers) {
instance[serializersSym][k] = serializers[k]
}
for (var bk in bindings.serializers) {
instance[serializersSym][bk] = bindings.serializers[bk]
}
} else instance[serializersSym] = serializers
if (bindings.hasOwnProperty('customLevels') === true) {
assertNoLevelCollisions(this.levels, bindings.customLevels)
instance.levels = mappings(bindings.customLevels, instance[useOnlyCustomLevelsSym])
genLsCache(instance)
}
instance[chindingsSym] = chindings
const childLevel = bindings.level || level
instance[setLevelSym](childLevel)
return instance
}
function bindings () {
const chindings = this[chindingsSym]
var chindingsJson = `{${chindings.substr(1)}}` // at least contains ,"pid":7068,"hostname":"myMac"
var bindingsFromJson = JSON.parse(chindingsJson)
delete bindingsFromJson['pid']
delete bindingsFromJson['hostname']
return bindingsFromJson
}
function write (_obj, msg, num) {
const t = this[timeSym]()
const messageKey = this[messageKeySym]
const objError = _obj instanceof Error
var obj
if (_obj === undefined || _obj === null) {
obj = {}
obj[messageKey] = msg
} else {
obj = Object.assign({}, _obj)
if (msg) {
obj[messageKey] = msg
} else if (objError) {
obj[messageKey] = _obj.message
}
if (objError) {
obj.stack = _obj.stack
if (!obj.type) {
obj.type = 'Error'
}
}
}
const s = this[asJsonSym](obj, num, t, objError)
const stream = this[streamSym]
if (stream[needsMetadataGsym] === true) {
stream.lastLevel = num
// TODO remove in the next major release,
// it is not needed anymore
stream.lastMsg = msg
stream.lastObj = obj
stream.lastTime = t.slice(this[timeSliceIndexSym])
stream.lastLogger = this // for child loggers
}
if (stream instanceof SonicBoom) stream.write(s)
else stream.write(flatstr(s))
}
function flush () {
const stream = this[streamSym]
if ('flush' in stream) stream.flush()
}