From abdba8e56fd060ddc7ad7aac719027e8e1eca501 Mon Sep 17 00:00:00 2001 From: Emile Cantin Date: Thu, 26 Sep 2013 14:55:20 -0400 Subject: [PATCH] Added logic to serialize Error objects correctly This should fix #97. --- lib/appenders/clustered.js | 10 +++++++++- lib/appenders/multiprocess.js | 5 +++++ test/clusteredAppender-test.js | 7 +++++++ test/multiprocess-test.js | 5 +++++ 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/appenders/clustered.js b/lib/appenders/clustered.js index f56e89a..aae3a4c 100755 --- a/lib/appenders/clustered.js +++ b/lib/appenders/clustered.js @@ -7,6 +7,14 @@ var log4js = require('../log4js'); * Takes a loggingEvent object, returns string representation of it. */ function serializeLoggingEvent(loggingEvent) { + // JSON.stringify(new Error('test')) returns {}, which is not really useful for us. + // The following allows us to serialize errors correctly. + for (var i = 0; i < loggingEvent.data.length; i++) { + var item = loggingEvent.data[i]; + if (item && item.stack && JSON.stringify(item) === '{}') { // Validate that we really are in this case + loggingEvent.data[i] = {stack : item.stack}; + } + } return JSON.stringify(loggingEvent); } @@ -115,4 +123,4 @@ function configure(config, options) { } exports.appender = createAppender; -exports.configure = configure; \ No newline at end of file +exports.configure = configure; diff --git a/lib/appenders/multiprocess.js b/lib/appenders/multiprocess.js index 2a1d56f..4444146 100644 --- a/lib/appenders/multiprocess.js +++ b/lib/appenders/multiprocess.js @@ -94,6 +94,11 @@ function workerAppender(config) { } function write(loggingEvent) { + // JSON.stringify(new Error('test')) returns {}, which is not really useful for us. + // The following allows us to serialize errors correctly. + if (loggingEvent && loggingEvent.stack && JSON.stringify(loggingEvent) === '{}') { // Validate that we really are in this case + loggingEvent = {stack : loggingEvent.stack}; + } socket.write(JSON.stringify(loggingEvent), 'utf8'); socket.write(END_MSG, 'utf8'); } diff --git a/test/clusteredAppender-test.js b/test/clusteredAppender-test.js index a0dd5fb..a8cafa3 100755 --- a/test/clusteredAppender-test.js +++ b/test/clusteredAppender-test.js @@ -97,6 +97,7 @@ vows.describe('log4js cluster appender').addBatch({ // Actual test - log message using masterAppender workerAppender(new LoggingEvent('wovs', 'Info', ['workerAppender test'])); + workerAppender(new LoggingEvent('wovs', 'Info', [new Error('Error test')])); var returnValue = { registeredProcessEvents: registeredProcessEvents, @@ -109,6 +110,12 @@ vows.describe('log4js cluster appender').addBatch({ "worker appender should call process.send" : function(topic) { assert.equal(topic.registeredProcessEvents[0].type, '::log-message'); assert.equal(JSON.parse(topic.registeredProcessEvents[0].event).data[0], "workerAppender test"); + }, + + "worker should serialize an Error correctly" : function(topic) { + var expected = { stack: 'Error: Error test\n at Object.vows.describe.addBatch.when in worker mode.topic (/home/vagrant/log4js-node/test/clusteredAppender-test.js:100:53)\n at run (/home/vagrant/log4js-node/node_modules/vows/lib/vows/suite.js:134:35)\n at EventEmitter.Suite.runBatch.callback (/home/vagrant/log4js-node/node_modules/vows/lib/vows/suite.js:234:40)\n at EventEmitter.emit (events.js:126:20)\n at EventEmitter.vows.describe.options.Emitter.emit (/home/vagrant/log4js-node/node_modules/vows/lib/vows.js:237:24)\n at Suite.runBatch.topic (/home/vagrant/log4js-node/node_modules/vows/lib/vows/suite.js:169:45)\n at process.startup.processNextTick.process._tickCallback (node.js:245:9)' }; + assert.equal(topic.registeredProcessEvents[1].type, '::log-message'); + assert.equal(JSON.stringify(JSON.parse(topic.registeredProcessEvents[1].event).data[0]), JSON.stringify(expected)); } } diff --git a/test/multiprocess-test.js b/test/multiprocess-test.js index 600cae5..e808d7f 100644 --- a/test/multiprocess-test.js +++ b/test/multiprocess-test.js @@ -75,6 +75,7 @@ vows.describe('Multiprocess Appender').addBatch({ appender('after error, before connect'); fakeNet.cbs.connect(); appender('after error, after connect'); + appender(new Error('Error test')); return fakeNet; }, @@ -98,6 +99,10 @@ vows.describe('Multiprocess Appender').addBatch({ assert.equal(net.data[6], JSON.stringify('after error, after connect')); assert.equal(net.data[7], '__LOG4JS__'); assert.equal(net.createConnectionCalled, 2); + }, + 'should serialize an Error correctly': function(net) { + var expected = { stack: 'Error: Error test\n at Object.vows.describe.addBatch.worker.topic (/home/vagrant/log4js-node/test/multiprocess-test.js:78:13)\n at run (/home/vagrant/log4js-node/node_modules/vows/lib/vows/suite.js:134:35)\n at EventEmitter.Suite.runBatch.callback (/home/vagrant/log4js-node/node_modules/vows/lib/vows/suite.js:234:40)\n at EventEmitter.emit (events.js:126:20)\n at EventEmitter.vows.describe.options.Emitter.emit (/home/vagrant/log4js-node/node_modules/vows/lib/vows.js:237:24)\n at Suite.runBatch.topic (/home/vagrant/log4js-node/node_modules/vows/lib/vows/suite.js:169:45)\n at process.startup.processNextTick.process._tickCallback (node.js:245:9)' }; + assert.equal(net.data[8], JSON.stringify(expected)); } }, 'worker with timeout': {