Skip to content

Commit 6c71bc9

Browse files
authored
Add bindings method to browser logger (#2504)
1 parent b557f72 commit 6c71bc9

2 files changed

Lines changed: 137 additions & 6 deletions

File tree

‎browser.js‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ function levelToValue (level, logger) {
4949
const baseLogFunctionSymbol = Symbol('pino.logFuncs')
5050
const hierarchySymbol = Symbol('pino.hierarchy')
5151

52+
const ownBindingsSymbol = Symbol('pino.ownBindings')
53+
const bindingsSymbol = Symbol('pino.bindings')
54+
5255
const logFallbackMap = {
5356
error: 'log',
5457
fatal: 'error',
@@ -122,6 +125,8 @@ function pino (opts) {
122125
// setup root hierarchy entry
123126
appendChildLogger({}, logger)
124127

128+
logger[bindingsSymbol] = {}
129+
125130
Object.defineProperty(logger, 'levelVal', {
126131
get: getLevelVal
127132
})
@@ -166,8 +171,14 @@ function pino (opts) {
166171
logger._serialize = serialize
167172
logger._stdErrSerialize = stdErrSerialize
168173
logger.child = function (...args) { return child.call(this, setOpts, ...args) }
174+
175+
logger.bindings = function () {
176+
return Object.assign({}, this[bindingsSymbol])
177+
}
178+
169179
logger.setBindings = function (newBindings) {
170-
this.bindings = Object.assign({}, this.bindings, newBindings)
180+
this[ownBindingsSymbol] = Object.assign({}, this[ownBindingsSymbol], newBindings)
181+
this[bindingsSymbol] = Object.assign({}, this[bindingsSymbol], newBindings)
171182
if (this._logEvent) {
172183
this._logEvent.bindings.push(newBindings)
173184
}
@@ -224,7 +235,13 @@ function pino (opts) {
224235
this._childLevel = (parent._childLevel | 0) + 1
225236

226237
// make sure bindings are available in the `set` function
227-
this.bindings = bindings
238+
this[ownBindingsSymbol] = bindings
239+
240+
this[bindingsSymbol] = Object.assign(
241+
{},
242+
parent[bindingsSymbol],
243+
bindings
244+
)
228245

229246
if (childSerializers) {
230247
this.serializers = childSerializers
@@ -295,16 +312,16 @@ pino.stdTimeFunctions = Object.assign({}, { nullTime, epochTime, unixTime, isoTi
295312

296313
function getBindingChain (logger) {
297314
const bindings = []
298-
if (logger.bindings) {
299-
bindings.push(logger.bindings)
315+
if (logger[ownBindingsSymbol]) {
316+
bindings.push(logger[ownBindingsSymbol])
300317
}
301318

302319
// traverse up the tree to get all bindings
303320
let hierarchy = logger[hierarchySymbol]
304321
while (hierarchy.parent) {
305322
hierarchy = hierarchy.parent
306-
if (hierarchy.logger.bindings) {
307-
bindings.push(hierarchy.logger.bindings)
323+
if (hierarchy.logger[ownBindingsSymbol]) {
324+
bindings.push(hierarchy.logger[ownBindingsSymbol])
308325
}
309326
}
310327

‎test/browser-bindings.test.js‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
'use strict'
2+
3+
const test = require('tape')
4+
const pino = require('../browser')
5+
6+
test('bindings returns an empty object on the root logger', ({ end, same }) => {
7+
const instance = pino({ browser: {} })
8+
9+
same(instance.bindings(), {})
10+
11+
end()
12+
})
13+
14+
test('bindings contains child bindings', ({ end, same }) => {
15+
const instance = pino({ browser: {} })
16+
const child = instance.child({ foo: 'bar' })
17+
18+
same(child.bindings(), { foo: 'bar' })
19+
20+
end()
21+
})
22+
23+
test('bindings contains bindings from nested children', ({ end, same }) => {
24+
const instance = pino({ browser: {} })
25+
const child = instance
26+
.child({ foo: 'bar' })
27+
.child({ a: 2 })
28+
29+
same(child.bindings(), { foo: 'bar', a: 2 })
30+
31+
end()
32+
})
33+
34+
test('child bindings overwrite parent bindings with the same key', ({ end, same }) => {
35+
const instance = pino({ browser: {} })
36+
const child = instance
37+
.child({ foo: 'bar' })
38+
.child({ foo: 'baz' })
39+
40+
same(child.bindings(), { foo: 'baz' })
41+
42+
end()
43+
})
44+
45+
test('bindings contains bindings added with setBindings', ({ end, same }) => {
46+
const instance = pino({ browser: {} })
47+
48+
instance.setBindings({ foo: 'bar' })
49+
50+
same(instance.bindings(), { foo: 'bar' })
51+
52+
end()
53+
})
54+
55+
test('newly set bindings overwrite old bindings', ({ end, same }) => {
56+
const instance = pino({ browser: {} })
57+
58+
instance.setBindings({ foo: 'bar' })
59+
instance.setBindings({ foo: 'baz' })
60+
61+
same(instance.bindings(), { foo: 'baz' })
62+
63+
end()
64+
})
65+
66+
test('setBindings on child merges with child bindings', ({ end, same }) => {
67+
const instance = pino({ browser: {} })
68+
const child = instance.child({ child: true })
69+
70+
child.setBindings({ later: 'yes' })
71+
72+
same(child.bindings(), {
73+
child: true,
74+
later: 'yes'
75+
})
76+
77+
end()
78+
})
79+
80+
test('child inherits bindings set on parent before child creation', ({ end, same }) => {
81+
const instance = pino({ browser: {} })
82+
83+
instance.setBindings({ foo: 'bar' })
84+
85+
const child = instance.child({})
86+
87+
same(child.bindings(), { foo: 'bar' })
88+
89+
end()
90+
})
91+
92+
test('child does not inherit bindings set on parent after child creation', ({ end, same }) => {
93+
const instance = pino({ browser: {} })
94+
const child = instance.child({})
95+
96+
instance.setBindings({ foo: 'bar' })
97+
98+
same(instance.bindings(), { foo: 'bar' })
99+
same(child.bindings(), {})
100+
101+
end()
102+
})
103+
104+
test('bindings returns a copy of the bindings', ({ end, same }) => {
105+
const instance = pino({ browser: {} })
106+
instance.setBindings({ foo: 'bar' })
107+
108+
const bindings = instance.bindings()
109+
bindings.foo = 'baz'
110+
111+
same(instance.bindings(), { foo: 'bar' })
112+
113+
end()
114+
})

0 commit comments

Comments
 (0)