diff --git a/lib/bunyan.js b/lib/bunyan.js index 31a67fa..586a34f 100644 --- a/lib/bunyan.js +++ b/lib/bunyan.js @@ -793,13 +793,16 @@ function mkLogEmitter(minLevel) { var excludeFields; if (args[0] instanceof Error) { // `log.(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]; } else { msgArgs = Array.prototype.slice.call(args, 1); - } + } } else if (typeof (args[0]) !== 'object' && args[0] !== null || Array.isArray(args[0])) { // `log.(msg, ...)` diff --git a/test/serializers.test.js b/test/serializers.test.js index f7862eb..6f8ddcf 100644 --- a/test/serializers.test.js +++ b/test/serializers.test.js @@ -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 = [];