use the correct error serializer
This commit is contained in:
parent
cb703ef474
commit
576e500b11
2 changed files with 44 additions and 2 deletions
|
@ -793,7 +793,10 @@ function mkLogEmitter(minLevel) {
|
|||
var excludeFields;
|
||||
if (args[0] instanceof Error) {
|
||||
// `log.<level>(err, ...)`
|
||||
fields = {err: errSerializer(args[0])};
|
||||
fields = {
|
||||
// if a serializer is defined for err use it else fallback on the default serializer
|
||||
err: log.serializers.err ? log.serializers.err(args[0]) : Logger.stdSerializers.err(args[0])
|
||||
};
|
||||
excludeFields = {err: true};
|
||||
if (args.length === 1) {
|
||||
msgArgs = [fields.err.message];
|
||||
|
|
|
@ -181,6 +181,45 @@ test('err serializer', function (t) {
|
|||
t.end();
|
||||
});
|
||||
|
||||
test('err serializer: custom serializer', function (t) {
|
||||
var records = [];
|
||||
|
||||
function customSerializer(err) {
|
||||
return {
|
||||
message: err.message,
|
||||
name: err.name,
|
||||
stack: err.stack,
|
||||
beep: err.beep
|
||||
};
|
||||
}
|
||||
|
||||
var log = bunyan.createLogger({
|
||||
name: 'serializer-test',
|
||||
streams: [
|
||||
{
|
||||
stream: new CapturingStream(records),
|
||||
type: 'raw'
|
||||
}
|
||||
],
|
||||
serializers: {
|
||||
err: customSerializer
|
||||
}
|
||||
});
|
||||
|
||||
var e1 = new Error('message1');
|
||||
e1.beep = 'bop';
|
||||
var e2 = new Error('message2');
|
||||
var errs = [e1, e2];
|
||||
|
||||
|
||||
|
||||
for (var i = 0; i < errs.length; i++) {
|
||||
log.info(errs[i]);
|
||||
t.equal(records[i].err.message, errs[i].message);
|
||||
t.equal(records[i].err.beep, errs[i].beep);
|
||||
}
|
||||
t.end();
|
||||
});
|
||||
|
||||
test('err serializer: long stack', function (t) {
|
||||
var records = [];
|
||||
|
|
Loading…
Reference in a new issue