Skip to content

Commit

Permalink
[fix] Handle Error instances in a sane way since their properties are…
Browse files Browse the repository at this point in the history
… non-enumerable __by default.__ Fixes #280.
  • Loading branch information
indexzero committed Dec 30, 2014
1 parent 03bd8d6 commit ad2d5e1
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/winston/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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;

//
Expand Down Expand Up @@ -155,7 +162,7 @@ exports.log = function (options) {
if (timestamp) {
output.timestamp = timestamp;
}

if (options.logstash === true) {
// use logstash format
var logstashOutput = {};
Expand Down Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions test/logger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit ad2d5e1

Please sign in to comment.