Skip to content

Commit

Permalink
Fixes #66: Now children can get serializers injected. Improved test a…
Browse files Browse the repository at this point in the history
…round serializers rules
  • Loading branch information
dgonzalez committed Jul 25, 2016
1 parent 167ce3f commit 86b0123
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
4 changes: 2 additions & 2 deletions pino.js
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ Pino.prototype.child = function child (bindings) {
var key
for (key in bindings) {
value = bindings[key]
if (key !== 'level' && bindings.hasOwnProperty(key) && value !== undefined) {
if (['level', 'serializers'].indexOf(key) === -1 && bindings.hasOwnProperty(key) && value !== undefined) {
value = this.serializers[key] ? this.serializers[key](value) : value
data += '"' + key + '":' + this.stringify(value) + ','
}
Expand All @@ -258,7 +258,7 @@ Pino.prototype.child = function child (bindings) {
return new Pino(
bindings.level || this.level,
this.stream,
this.serializers,
bindings.serializers || this.serializers,
this.stringify,
this.end,
this.name,
Expand Down
70 changes: 70 additions & 0 deletions test/serializers.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict'
var test = require('tap').test
var pino = require('../')
var sink = require('./helper').sink

var parentSerializers = {
test: function () { return 'parent' }
}

var childSerializers = {
test: function () { return 'child' }
}

test('child does not override parent serializers', function (t) {
t.plan(2)

var parent = pino({ serializers: parentSerializers })
var child = parent.child({ serializers: childSerializers })

t.deepEqual(parent.serializers, parentSerializers)
t.deepEqual(child.serializers, childSerializers)
})

test('children inherit parent serializers', function (t) {
t.plan(1)

var parent = pino({ serializers: parentSerializers })
var child = parent.child({a: 'property'})

t.deepEqual(child.serializers, parentSerializers)
})

test('children serializers get called', function (t) {
t.plan(1)

var parent = pino({
test: 'this'
}, sink(function (chunk, enc, cb) {
cb()
}))

var child = parent.child({ 'a': 'property', serializers: childSerializers })

child.serializers.test = function () {
t.ok('serializer called')
return 'called'
}

child.fatal({test: 'test'})
})

test('children serializers get called when inherited from parent', function (t) {
t.plan(1)

var parent = pino({
test: 'this',
serializers: childSerializers
}, sink(function (chunk, enc, cb) {
cb()
}))

var child = parent.child({ 'a': 'property' })

child.serializers.test = function () {
t.ok('serializer called')
return 'called'
}

child.fatal({test: 'test'})
})

0 comments on commit 86b0123

Please sign in to comment.