Merge pull request #158 from emilecantin/master
Added logic to serialize Error objects correctly
This commit is contained in:
commit
d0661322aa
@ -7,6 +7,14 @@ var log4js = require('../log4js');
|
|||||||
* Takes a loggingEvent object, returns string representation of it.
|
* Takes a loggingEvent object, returns string representation of it.
|
||||||
*/
|
*/
|
||||||
function serializeLoggingEvent(loggingEvent) {
|
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);
|
return JSON.stringify(loggingEvent);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -115,4 +123,4 @@ function configure(config, options) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
exports.appender = createAppender;
|
exports.appender = createAppender;
|
||||||
exports.configure = configure;
|
exports.configure = configure;
|
||||||
|
@ -94,6 +94,11 @@ function workerAppender(config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function write(loggingEvent) {
|
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(JSON.stringify(loggingEvent), 'utf8');
|
||||||
socket.write(END_MSG, 'utf8');
|
socket.write(END_MSG, 'utf8');
|
||||||
}
|
}
|
||||||
|
@ -97,6 +97,7 @@ vows.describe('log4js cluster appender').addBatch({
|
|||||||
|
|
||||||
// Actual test - log message using masterAppender
|
// Actual test - log message using masterAppender
|
||||||
workerAppender(new LoggingEvent('wovs', 'Info', ['workerAppender test']));
|
workerAppender(new LoggingEvent('wovs', 'Info', ['workerAppender test']));
|
||||||
|
workerAppender(new LoggingEvent('wovs', 'Info', [new Error('Error test')]));
|
||||||
|
|
||||||
var returnValue = {
|
var returnValue = {
|
||||||
registeredProcessEvents: registeredProcessEvents,
|
registeredProcessEvents: registeredProcessEvents,
|
||||||
@ -109,6 +110,14 @@ vows.describe('log4js cluster appender').addBatch({
|
|||||||
"worker appender should call process.send" : function(topic) {
|
"worker appender should call process.send" : function(topic) {
|
||||||
assert.equal(topic.registeredProcessEvents[0].type, '::log-message');
|
assert.equal(topic.registeredProcessEvents[0].type, '::log-message');
|
||||||
assert.equal(JSON.parse(topic.registeredProcessEvents[0].event).data[0], "workerAppender test");
|
assert.equal(JSON.parse(topic.registeredProcessEvents[0].event).data[0], "workerAppender test");
|
||||||
|
},
|
||||||
|
|
||||||
|
"worker should serialize an Error correctly" : function(topic) {
|
||||||
|
assert.equal(topic.registeredProcessEvents[1].type, '::log-message');
|
||||||
|
assert(JSON.parse(topic.registeredProcessEvents[1].event).data[0].stack);
|
||||||
|
var actual = JSON.parse(topic.registeredProcessEvents[1].event).data[0].stack;
|
||||||
|
var expectedRegex = /^Error: Error test/;
|
||||||
|
assert(actual.match(expectedRegex), "Expected: \n\n " + actual + "\n\n to match " + expectedRegex);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,7 @@ vows.describe('Multiprocess Appender').addBatch({
|
|||||||
appender('after error, before connect');
|
appender('after error, before connect');
|
||||||
fakeNet.cbs.connect();
|
fakeNet.cbs.connect();
|
||||||
appender('after error, after connect');
|
appender('after error, after connect');
|
||||||
|
appender(new Error('Error test'));
|
||||||
|
|
||||||
return fakeNet;
|
return fakeNet;
|
||||||
},
|
},
|
||||||
@ -98,6 +99,13 @@ vows.describe('Multiprocess Appender').addBatch({
|
|||||||
assert.equal(net.data[6], JSON.stringify('after error, after connect'));
|
assert.equal(net.data[6], JSON.stringify('after error, after connect'));
|
||||||
assert.equal(net.data[7], '__LOG4JS__');
|
assert.equal(net.data[7], '__LOG4JS__');
|
||||||
assert.equal(net.createConnectionCalled, 2);
|
assert.equal(net.createConnectionCalled, 2);
|
||||||
|
},
|
||||||
|
'should serialize an Error correctly': function(net) {
|
||||||
|
assert(JSON.parse(net.data[8]).stack, "Expected:\n\n" + net.data[8] + "\n\n to have a 'stack' property");
|
||||||
|
var actual = JSON.parse(net.data[8]).stack;
|
||||||
|
var expectedRegex = /^Error: Error test/;
|
||||||
|
assert(actual.match(expectedRegex), "Expected: \n\n " + actual + "\n\n to match " + expectedRegex);
|
||||||
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
'worker with timeout': {
|
'worker with timeout': {
|
||||||
|
Loading…
Reference in New Issue
Block a user