Node-bunyan: Passing an Error and other fields using `err` logs empty object

Created on 22 Apr 2016  路  11Comments  路  Source: trentm/node-bunyan

When logging errors in an object along with other fields as described in the last option of these examples the error is logged as an empty object:

const log = bunyan.createLogger({name: 'testLogger'});
const error = new Error('Bad stuff happened');

log.fatal(error);
log.fatal(error, 'Replaced the message');
log.fatal({err: error, field1: 'value1'}, 'Error with additional fields');

The first two cases work as expected, but in the final case the logged output is:

08:35:23.544Z FATAL testLogger: Error with additional fields (err={}, field1=value1)

The logged JSON contains the err property as an empty object.

Type-Doc Component-Lib Resolution-Fixed

Most helpful comment

1 year later this still happens.

```

logger.error({ err: new Error('test') });
{"name":"foo","pid":42987,"level":50,"err":{},"msg":"test","time":"2017-05-01T19:24:10.699Z","v":0}
````

All 11 comments

Seems to be related to #7 and #290.

Not sure if this is a bug or not, but a workaround is here #369

1 year later this still happens.

```

logger.error({ err: new Error('test') });
{"name":"foo","pid":42987,"level":50,"err":{},"msg":"test","time":"2017-05-01T19:24:10.699Z","v":0}
````

Sorry for the delay in responding. @marnusw Thanks for pointing out that this is related to #7.

The issue is that an 'err' serializer isn't given. I'm not sure about a Bunyan logger having an implicit err serializer. #7 is about doc improvements on how to move from log.info(err, 'hi') to log.info({err: err, other: field}, 'hi')

So either this is a doc and usage issue (including perhaps a call out in a troubleshooting or "common surprises" section)... or Bunyan (perhaps only in 2.x) considers having implicit default serializers. That is also a big leap for compatibility.

// Current behaviour:
var log = bunyan.createLogger({name: 'foo'}) // implicit no serializers
var log = bunyan.createLogger({name: 'foo', serializers: bunyan.stdSerializers}) // explicit common "standard" serializers
// TODO: explore the restify case
//      - and app-specific serializers
//      - and a library ensuring a serializer for its own usage

// Possible behaviour with default serializers
var log = bunyan.createLogger({name: 'foo', serializers: null}) // explicit no serializers
var log = bunyan.createLogger({name: 'foo'}) // implicit default serializers
// TODO: how do the cases above change?

If making this change, I think at most we'd have the err serializer on
the default set.

The README does not state that you need to pass in the err serializer for this case.

https://github.com/trentm/node-bunyan#log-method-api

log.info({foo: 'bar', err: err}, 'some msg about this error');
                // To pass in an Error *and* other fields, use the `err`
                // field name for the Error instance.

@landau Yes, totally fair. The docs suck here.

Also, the changelog mentions a fix for this. https://github.com/trentm/node-bunyan/blob/master/CHANGES.md#182

@landau Yes, that was a different issue.

Updating the docs to show it is required to do

var log = bunyan.createLogger({name: 'foo', serializers: bunyan.stdSerializers}) // explicit common "standard" serializers

should be sufficient to resolve this issue for v1 in my opinion. Whether standard serializers are added by default in v2 is then a different matter entirely.

Thanks for the feedback @trentm.

@marnusw Thanks. I've tried to improve the README with the commit above.

We're having this issue, meaning that our err object is always empty.

We can't use the fix of only using bunyan.stdSerializers because we use other serializers. Strangely, this doesn't seem to work:

    serializers: {
      req: logSerializers.req, // our own serializer
      err: bunyan.stdSerializers.err
    }

Is there any fix?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

raybooysen picture raybooysen  路  3Comments

aleybovich picture aleybovich  路  11Comments

mouhong picture mouhong  路  6Comments

simonexmachina picture simonexmachina  路  8Comments

shaucorp picture shaucorp  路  5Comments