From ad2d5e1f2ca0d9d56620d25251d6dd5f3d570601 Mon Sep 17 00:00:00 2001 From: indexzero Date: Tue, 7 Oct 2014 14:01:42 -0400 Subject: [PATCH] [fix] Handle Error instances in a sane way since their properties are non-enumerable __by default.__ Fixes #280. --- lib/winston/common.js | 21 ++++++++++++++------- test/logger-test.js | 9 +++++++++ 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/lib/winston/common.js b/lib/winston/common.js index 5ee24c936..5d14f2f5e 100644 --- a/lib/winston/common.js +++ b/lib/winston/common.js @@ -75,8 +75,13 @@ exports.longestElement = function (xs) { // i.e. JSON objects that are either literals or objects (no Arrays, etc) // exports.clone = function (obj) { - // we only need to clone refrence types (Object) - if (!(obj instanceof Object)) { + // + // We only need to clone refrence types (Object) + // + if (obj instanceof Error) { + return obj; + } + else if (!(obj instanceof Object)) { return obj; } else if (obj instanceof Date) { @@ -119,10 +124,12 @@ exports.clone = function (obj) { // exports.log = function (options) { var timestampFn = typeof options.timestamp === 'function' - ? options.timestamp - : exports.timestamp, + ? options.timestamp + : exports.timestamp, timestamp = options.timestamp ? timestampFn() : null, - meta = options.meta !== undefined || options.meta !== null ? exports.clone(cycle.decycle(options.meta)) : null, + meta = options.meta !== null && options.meta !== undefined && !(options.meta instanceof Error) + ? exports.clone(cycle.decycle(options.meta)) + : options.meta || null, output; // @@ -155,7 +162,7 @@ exports.log = function (options) { if (timestamp) { output.timestamp = timestamp; } - + if (options.logstash === true) { // use logstash format var logstashOutput = {}; @@ -317,7 +324,7 @@ exports.tailFile = function tail(options, callback) { if (options.start === -1) { delete options.start; } - + stream.on('data', function (data) { var data = (buff + data).split(/\n+/), l = data.length - 1, diff --git a/test/logger-test.js b/test/logger-test.js index 2f3079c0c..9a1e538d6 100755 --- a/test/logger-test.js +++ b/test/logger-test.js @@ -276,6 +276,15 @@ vows.describe('winton/logger').addBatch({ ] }), "the log() method": { + "when passed an Error object as meta": { + topic: function (logger) { + logger.once('logging', this.callback); + logger.log('info', 'An error happened: ', new Error('I am something bad')); + }, + "should respond with a proper error output": function (transport, level, msg, meta) { + assert.instanceOf(meta, Error); + } + }, "when passed a string placeholder": { topic: function (logger) { logger.once('logging', this.callback);