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

The serializer object should not have a prototype #527

Merged
merged 1 commit into from
Oct 3, 2018
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
2 changes: 1 addition & 1 deletion lib/proto.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function child (bindings) {
const chindings = asChindings(this, bindings)
const instance = Object.create(this)
if (bindings.hasOwnProperty('serializers') === true) {
instance[serializersSym] = {}
instance[serializersSym] = Object.create(null)
for (var k in serializers) {
instance[serializersSym][k] = serializers[k]
}
Expand Down
4 changes: 4 additions & 0 deletions lib/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,10 @@ function asJson (obj, msg, num, time) {
case 'string':
value = (stringifiers[key] || asString)(value)
break
case 'function':
// ignore functions
value = undefined
break
default:
value = (stringifiers[key] || stringify)(value)
}
Expand Down
12 changes: 8 additions & 4 deletions pino.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict'
const os = require('os')
const serializers = require('pino-std-serializers')
const stdSerializers = require('pino-std-serializers')
const SonicBoom = require('sonic-boom')
const redaction = require('./lib/redaction')
const time = require('./lib/time')
Expand Down Expand Up @@ -33,15 +33,17 @@ const {
const { epochTime, nullTime } = time
const { pid } = process
const hostname = os.hostname()
const defaultErrorSerializer = serializers.err
const defaultErrorSerializer = stdSerializers.err
const defaultOptions = {
level: 'info',
useLevelLabels: false,
messageKey: 'msg',
enabled: true,
prettyPrint: false,
base: { pid, hostname },
serializers: { err: defaultErrorSerializer },
serializers: Object.assign(Object.create(null), {
err: defaultErrorSerializer
}),
timestamp: epochTime,
name: undefined,
redact: null,
Expand All @@ -52,6 +54,8 @@ const defaultOptions = {

const normalize = createArgsNormalizer(defaultOptions)

const serializers = Object.assign(Object.create(null), stdSerializers)

function pino (...args) {
const { opts, stream } = normalize(...args)
const {
Expand Down Expand Up @@ -119,7 +123,7 @@ pino.extreme = (dest = process.stdout.fd) => new SonicBoom(dest, 4096)
pino.destination = (dest = process.stdout.fd) => new SonicBoom(dest)
pino.final = final
pino.levels = mappings()
pino.stdSerializers = Object.assign({}, serializers)
pino.stdSerializers = serializers
pino.stdTimeFunctions = Object.assign({}, time)
pino.symbols = symbols
pino.version = version
Expand Down
21 changes: 21 additions & 0 deletions test/error.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,3 +153,24 @@ test('stack is rendered as any other property if it\'s not a string', t => {
instance.level = name
instance[name](err)
})

test('correctly ignores toString on errors', async ({ same }) => {
const err = new Error('myerror')
err.toString = () => undefined
const stream = sink()
const instance = pino({
test: 'this'
}, stream)
instance.fatal(err)
const result = await once(stream, 'data')
delete result.time
same(result, {
pid: pid,
hostname: hostname,
level: 60,
type: 'Error',
msg: err.message,
stack: err.stack,
v: 1
})
})
9 changes: 8 additions & 1 deletion test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ function once (emitter, name) {
}

function sink (func) {
const result = split(JSON.parse)
const result = split((data) => {
try {
return JSON.parse(data)
} catch (err) {
console.log(err)
console.log(data)
}
})
if (func) result.pipe(writer.obj(func))
return result
}
Expand Down