Added logic to serialize Error objects correctly

This should fix #97.
This commit is contained in:
Emile Cantin
2013-09-26 14:55:20 -04:00
parent 093f693232
commit abdba8e56f
4 changed files with 26 additions and 1 deletions

View File

@@ -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;
exports.configure = configure;

View File

@@ -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');
}