Skip to content

Commit

Permalink
Merge pull request #1420 from log4js-node/1417-typeerror-cannot-conve…
Browse files Browse the repository at this point in the history
…rt-object-to-primitive-value-in-cluster-mode

fix(LoggingEvent): serde for object with null prototype
  • Loading branch information
lamweili committed Apr 4, 2024
2 parents f5cac41 + e1efb82 commit 4027fc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/LoggingEvent.js
Expand Up @@ -22,7 +22,11 @@ class SerDe {

canSerialise(key) {
if (typeof key === 'string') return false;
return key in this.serMap;
try {
return key in this.serMap;
} catch (e) {
return false;
}
}

serialise(key) {
Expand Down
10 changes: 9 additions & 1 deletion test/tap/LoggingEvent-test.js
Expand Up @@ -20,6 +20,8 @@ test('LoggingEvent', (batch) => {
});

batch.test('should serialise to flatted', (t) => {
const nullPrototype = Object.create(null);
nullPrototype.hello = 'world';
const event = new LoggingEvent(
'cheese',
levels.DEBUG,
Expand All @@ -33,6 +35,7 @@ test('LoggingEvent', (batch) => {
'-Infinity',
undefined,
'undefined',
nullPrototype,
],
{
user: 'bob',
Expand All @@ -44,7 +47,7 @@ test('LoggingEvent', (batch) => {
t.equal(rehydratedEvent.startTime, '2018-02-04T18:30:23.010Z');
t.equal(rehydratedEvent.categoryName, 'cheese');
t.equal(rehydratedEvent.level.levelStr, 'DEBUG');
t.equal(rehydratedEvent.data.length, 9);
t.equal(rehydratedEvent.data.length, 10);
t.equal(rehydratedEvent.data[0], 'log message');
t.equal(rehydratedEvent.data[1], '__LOG4JS_NaN__');
t.equal(rehydratedEvent.data[2], 'NaN');
Expand All @@ -54,6 +57,11 @@ test('LoggingEvent', (batch) => {
t.equal(rehydratedEvent.data[6], '-Infinity');
t.equal(rehydratedEvent.data[7], '__LOG4JS_undefined__');
t.equal(rehydratedEvent.data[8], 'undefined');
t.equal(
Object.entries(rehydratedEvent.data[9]).length,
Object.entries(nullPrototype).length
);
t.equal(rehydratedEvent.data[9].hello, 'world');
t.equal(rehydratedEvent.context.user, 'bob');
t.end();
});
Expand Down

0 comments on commit 4027fc7

Please sign in to comment.