How to reproduce:
Environment:
node 5.7.0
bunyan 1.7.1
test.js:
var log = bunyan.createLogger({ name: 'app' });
log.error({ err: new Error('not work'), other_field: 123 }, 'Something went wrong...');
Then in shell:
node test.js | bunyan
Output:
[2016-03-08T11:27:11.291Z] ERROR: Something went wrong... (err={}, other_field:123)
If I pass Error object as root, like this:
log.error(new Error('not work'), 'Something went wrong...');
The output will be (expected):
[2016-03-08T11:34:23.388Z] ERROR: Something went wrong...
Error: not work
at Object.<anonymous> (/.../test.js:9:14)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Function.Module.runMain (module.js:447:10)
at startup (node.js:141:18)
at node.js:933:3
In both cases I passed in the Error object. But in the former case, it doesn't print the error stack trace while in the latter case it prints the error stack trace. I think the behaviors should be consistent, that is, both print the error stack trace.
When you pass an error as the "root", you gain the standard error logger by default.
Try to pass the standard serializers during createLogger. Should fix your issue
var log = bunyan.createLogger({
name: 'app',
serializers: bunyan.stdSerializers
});
I do agree the behaviour might feel a little inconsistent though...
@Khez your solution works. Thanks!
@Khez this workaround does not fix my problem, because I have to set serializers to other values:
serializers: {
error: bunyan.stdSerializers.err,
request: reqSerializer,
response: bunyan.stdSerializers.res
}
any suggestions?
Passing serializers as suggested worked for me, thanks 馃憤
Any thoughts on documenting the need to do this or what needs in bunyan core to handle this w/o explicit serializers? Reading the current README gives an impression this is handled out of the box -- spent a good amount of time digging into this, until finding this issue with a working fix :)
I just ran into this too, and the suggested workarounds above didn't fix it. I'm using node 6.9.1 in a docker container with pm2 running a process, and bunyan 1.8.5. Logging err directly works when running the app locally, but when in the container environment it's logged as an empty object.
This solved it for me -- instead of logger.error({err, other: 123}, 'foo'), use the stack property: logger.error({err: err.stack, other: 123}, 'foo'). Then the full stack trace starting with the actual error message message appears in the bunyan logs.
Sorry for the delay in responding. #398 and #7 are basically the same issue. The README example showing logging an error via log.info({err: err, ...}, 'foo'); didn't at all make it clear that that requires having an err serializer.
I tried to improve the README in https://github.com/trentm/node-bunyan/commit/e540a76424f56837c53e58b06298ffefbcbf92d9
Most helpful comment
When you pass an error as the "root", you gain the standard error logger by default.
Try to pass the standard serializers during createLogger. Should fix your issue
I do agree the behaviour might feel a little inconsistent though...