diff --git a/lib/log4js.js b/lib/log4js.js index 2a0e0b1..a840be2 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -279,9 +279,13 @@ module.exports = function (fileSystem, standardOutput, configPaths) { this.startTime = new Date(); this.categoryName = categoryName; this.message = message; - this.exception = exception; this.level = level; this.logger = logger; + if (exception && exception.message && exception.name) { + this.exception = exception; + } else if (exception) { + this.exception = new Error(exception); + } } /** diff --git a/test/logging.js b/test/logging.js index 0d48492..8e15e1d 100644 --- a/test/logging.js +++ b/test/logging.js @@ -29,6 +29,8 @@ vows.describe('log4js').addBatch({ logger.trace("Trace event 1"); logger.trace("Trace event 2"); logger.warn("Warning event"); + logger.error("Aargh!", new Error("Pants are on fire!")); + logger.error("Simulated CouchDB problem", JSON.stringify({ err: 127, cause: "incendiary underwear" })); return events; }, @@ -39,9 +41,20 @@ vows.describe('log4js').addBatch({ }, 'should not emit events of a lower level': function(events) { - assert.length(events, 2); + assert.length(events, 4); assert.equal(events[1].level.toString(), 'WARN'); - } + }, + + 'should include the error if passed in': function (events) { + assert.instanceOf(events[2].exception, Error); + assert.equal(events[2].exception.message, 'Pants are on fire!'); + }, + + 'should convert things that claim to be errors into Error objects': function (events) { + assert.instanceOf(events[3].exception, Error); + assert.equal(events[3].exception.message, '{"err":127,"cause":"incendiary underwear"}'); + }, + }, },