From 576e500b1171b6878e57690ef5377a9a830551eb Mon Sep 17 00:00:00 2001 From: Mihai Tomescu Date: Sat, 13 Sep 2014 20:12:09 -0400 Subject: [PATCH 1/2] use the correct error serializer --- lib/bunyan.js | 7 +++++-- test/serializers.test.js | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) 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 = []; From 92a4e2b81b67aaa8d6e641aa9961bafd83d2f0a6 Mon Sep 17 00:00:00 2001 From: Mihai Tomescu Date: Sat, 13 Sep 2014 20:16:46 -0400 Subject: [PATCH 2/2] removed empty lines --- test/serializers.test.js | 2 -- 1 file changed, 2 deletions(-) diff --git a/test/serializers.test.js b/test/serializers.test.js index 6f8ddcf..3c7578b 100644 --- a/test/serializers.test.js +++ b/test/serializers.test.js @@ -211,8 +211,6 @@ test('err serializer: custom serializer', function (t) { 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);