Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(bindings): allow setting of bindings #754

Merged
merged 3 commits into from
Dec 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' })
jsumners marked this conversation as resolved.
Show resolved Hide resolved
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' })
mcollina marked this conversation as resolved.
Show resolved Hide resolved
})

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