Skip to content

Commit

Permalink
feat(bindings): allow setting of bindings (#754)
Browse files Browse the repository at this point in the history
* feat(bindings): allow setting of bindings

* test(bindings): parent-child setBindings tests

* test(bindings): additional parent bindings check
  • Loading branch information
erikwlarsen authored and mcollina committed Dec 13, 2019
1 parent 6fe7476 commit 1586df3
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ const prototype = {
constructor,
child,
bindings,
setBindings,
flush,
isLevelEnabled,
version,
Expand Down Expand Up @@ -113,6 +114,11 @@ function bindings () {
return bindingsFromJson
}

function setBindings (newBindings) {
const chindings = asChindings(this, newBindings)
this[chindingsSym] = chindings
}

function write (_obj, msg, num) {
const t = this[timeSym]()
const messageKey = this[messageKeySym]
Expand Down
33 changes: 33 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,39 @@ test('bindings contain the name and the child bindings', async ({ same }) => {
same(instance.bindings(), { name: 'basicTest', foo: 'bar', a: 2 })
})

test('set bindings on instance', async ({ same }) => {
const instance = pino({ name: 'basicTest', level: 'info' })
instance.setBindings({ foo: 'bar' })
same(instance.bindings(), { name: 'basicTest', foo: 'bar' })
})

test('newly set bindings overwrite old bindings', async ({ same }) => {
const instance = pino({ name: 'basicTest', level: 'info', base: { foo: 'bar' } })
instance.setBindings({ foo: 'baz' })
same(instance.bindings(), { name: 'basicTest', foo: 'baz' })
})

test('set bindings on child instance', async ({ same }) => {
const child = pino({ name: 'basicTest', level: 'info' }).child({})
child.setBindings({ foo: 'bar' })
same(child.bindings(), { name: 'basicTest', foo: 'bar' })
})

test('child should have bindings set by parent', async ({ same }) => {
const instance = pino({ name: 'basicTest', level: 'info' })
instance.setBindings({ foo: 'bar' })
const child = instance.child({})
same(child.bindings(), { name: 'basicTest', foo: 'bar' })
})

test('child should not share bindings of parent set after child creation', async ({ same }) => {
const instance = pino({ name: 'basicTest', level: 'info' })
const child = instance.child({})
instance.setBindings({ foo: 'bar' })
same(instance.bindings(), { name: 'basicTest', foo: 'bar' })
same(child.bindings(), { name: 'basicTest' })
})

function levelTest (name, level) {
test(`${name} logs as ${level}`, async ({ is }) => {
const stream = sink()
Expand Down

0 comments on commit 1586df3

Please sign in to comment.