pinojs/pino-std-serializers

Error serializer custom context

ecdeveloper opened this issue · 1 comments

I'm trying to log an error object, that has some extra context. The error serializer ignores anything but type, message and stack. Would you consider extending the error serializer to make it hydrate the resulting _err object with the extra fields it may have? Or should I rather use the pino-std-serializers directly, serialize my error object directly, then hydrate it and then pass it to pino?

Example:

const logger = pino();

const err = new Error('Boom!');
err.foo = 'bar';

// Expecting to see `foo: bar` logged here, but it's not
logger.error(err);

Here's the interim solution I have:

const stdSerializers = require('pino-std-serializers');
const logger = pino();

// Assuming this error is created in a separate file, and passed into this file as an argument
const err = new Error('Boom!');
err.foo = 'bar';

const serializedErr = stdSerializers.err(err);
serializedErr.foo = err.foo;

pino.error(serializedErr);

I'm a bit puzzled, the serializer should be applied automatically. Have you tried calling logger.error({ err }) instead?