8000 Merge pull request #673 from log4js-node/stacktrace-bug · xyz-data/log4js-node@2c75220 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2c75220

Browse files
authored
Merge pull request log4js-node#673 from log4js-node/stacktrace-bug
fix(log4js-node#671): fixes serialisation of errors with properties
2 parents 29368cf + a901861 commit 2c75220

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

lib/log4js.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,8 @@ function serialise(logEvent) {
9393
// Validate that we really are in this case
9494
try {
9595
const logData = logEvent.data.map((e) => {
96-
if (e && e.stack && CircularJSON.stringify(e) === '{}') {
97-
e = { message: e.message, stack: e.stack };
96+
if (e && e.message && e.stack) {
97+
e = Object.assign({ message: e.message, stack: e.stack }, e);
9898
}
9999
return e;
100100
});
@@ -116,9 +116,9 @@ function deserialise(serialised) {
116116
event.startTime = new Date(event.startTime);
117117
event.level = config.levels.getLevel(event.level.levelStr);
118118
event.data = event.data.map((e) => {
119-
if (e && e.stack) {
120-
const fakeError = new Error(e.message);
121-
fakeError.stack = e.stack;
119+
if (e && e.message && e.stack) {
120+
const fakeError = new Error(e);
121+
Object.keys(e).forEach((key) => { fakeError[key] = e[key]; });
122122
e = fakeError;
123123
}
124124
return e;

test/tap/cluster-test.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,30 @@ if (cluster.isMaster) {
3636
test('cluster master', (batch) => {
3737
batch.test('events should be logged', (t) => {
3838
t.equal(logEvents.length, 3);
39+
3940
t.equal(logEvents[0].categoryName, 'master');
4041
t.equal(logEvents[0].pid, masterPid);
42+
4143
t.equal(logEvents[1].categoryName, 'worker');
4244
t.equal(logEvents[1].pid, workerPid);
45+
// serialising errors with stacks intact
4346
t.type(logEvents[1].data[1], 'Error');
4447
t.contains(logEvents[1].data[1].stack, 'Error: oh dear');
48+
// serialising circular references in objects
4549
t.type(logEvents[1].data[2], 'object');
4650
t.type(logEvents[1].data[2].me, 'object');
51+
// serialising errors with custom properties
52+
t.type(logEvents[1].data[3], 'Error');
53+
t.contains(logEvents[1].data[3].stack, 'Error: wtf');
54+
t.equal(logEvents[1].data[3].alert, 'chartreuse');
55+
// serialising things that are not errors, but look a bit like them
56+
t.type(logEvents[1].data[4], 'object');
57+
t.equal(logEvents[1].data[4].stack, 'this is not a stack trace');
58+
4759
t.equal(logEvents[2].categoryName, 'log4js');
4860
t.equal(logEvents[2].level.toString(), 'ERROR');
4961
t.equal(logEvents[2].data[0], 'Unable to parse log:');
62+
5063
t.end();
5164
});
5265

@@ -63,9 +76,15 @@ if (cluster.isMaster) {
6376
});
6477
} else {
6578
const workerLogger = log4js.getLogger('worker');
79+
// test for serialising circular references
6680
const circle = {};
6781
circle.me = circle;
68-
workerLogger.info('this is worker', new Error('oh dear'), circle);
82+
// test for serialising errors with their own properties
83+
const someError = new Error('wtf');
84+
someError.alert = 'chartreuse';
85+
// test for serialising things that look like errors but aren't.
86+
const notAnError = { stack: 'this is not a stack trace' };
87+
workerLogger.info('this is worker', new Error('oh dear'), circle, someError, notAnError);
6988
// can't run the test in the worker, things get weird
7089
process.send({
7190
type: '::testing',

0 commit comments

Comments
 (0)
0