Skip to content

Commit

Permalink
fix(cluster): added circular json dep
Browse files Browse the repository at this point in the history
  • Loading branch information
Gareth Jones committed Dec 11, 2017
1 parent ae97ae6 commit 8351896
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 7 deletions.
7 changes: 4 additions & 3 deletions lib/appenders/multiprocess.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const debug = require('debug')('log4js:multiprocess');
const net = require('net');
const CircularJSON = require('circular-json');

const END_MSG = '__LOG4JS__';

Expand All @@ -19,7 +20,7 @@ function logServer(config, actualAppender, levels) {
debug('deserialising log event');
let loggingEvent;
try {
loggingEvent = JSON.parse(msg);
loggingEvent = CircularJSON.parse(msg);
loggingEvent.startTime = new Date(loggingEvent.startTime);
loggingEvent.level = levels.getLevel(loggingEvent.level.levelStr);
} catch (e) {
Expand Down Expand Up @@ -98,13 +99,13 @@ function workerAppender(config) {
// The following allows us to serialize errors correctly.
// Validate that we really are in this case
const logData = loggingEvent.data.map((e) => {
if (e && e.stack && JSON.stringify(e) === '{}') {
if (e && e.stack && CircularJSON.stringify(e) === '{}') {
e = { stack: e.stack };
}
return e;
});
loggingEvent.data = logData;
socket.write(JSON.stringify(loggingEvent), 'utf8');
socket.write(CircularJSON.stringify(loggingEvent), 'utf8');
socket.write(END_MSG, 'utf8');
}

Expand Down
7 changes: 4 additions & 3 deletions lib/log4js.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
const debug = require('debug')('log4js:main');
const fs = require('fs');
const CircularJSON = require('circular-json');
const Configuration = require('./configuration');
const connectModule = require('./connect-logger');
const logger = require('./logger');
Expand Down Expand Up @@ -92,13 +93,13 @@ function serialise(logEvent) {
// Validate that we really are in this case
try {
const logData = logEvent.data.map((e) => {
if (e && e.stack && JSON.stringify(e) === '{}') {
if (e && e.stack && CircularJSON.stringify(e) === '{}') {
e = { message: e.message, stack: e.stack };
}
return e;
});
logEvent.data = logData;
return JSON.stringify(logEvent);
return CircularJSON.stringify(logEvent);
} catch (e) {
return serialise(new LoggingEvent(
'log4js',
Expand All @@ -111,7 +112,7 @@ function serialise(logEvent) {
function deserialise(serialised) {
let event;
try {
event = JSON.parse(serialised);
event = CircularJSON.parse(serialised);
event.startTime = new Date(event.startTime);
event.level = config.levels.getLevel(event.level.levelStr);
event.data = event.data.map((e) => {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"lib": "lib"
},
"dependencies": {
"circular-json": "^0.4.0",
"date-format": "^1.2.0",
"debug": "^3.1.0",
"semver": "^5.3.0",
Expand Down
6 changes: 5 additions & 1 deletion test/tap/cluster-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ if (cluster.isMaster) {
t.equal(logEvents[1].pid, workerPid);
t.type(logEvents[1].data[1], 'Error');
t.contains(logEvents[1].data[1].stack, 'Error: oh dear');
t.type(logEvents[1].data[2], 'object');
t.type(logEvents[1].data[2].me, 'object');
t.equal(logEvents[2].categoryName, 'log4js');
t.equal(logEvents[2].level.toString(), 'ERROR');
t.equal(logEvents[2].data[0], 'Unable to parse log:');
Expand All @@ -61,7 +63,9 @@ if (cluster.isMaster) {
});
} else {
const workerLogger = log4js.getLogger('worker');
workerLogger.info('this is worker', new Error('oh dear'));
const circle = {};
circle.me = circle;
workerLogger.info('this is worker', new Error('oh dear'), circle);
// can't run the test in the worker, things get weird
process.send({
type: '::testing',
Expand Down

0 comments on commit 8351896

Please sign in to comment.