Skip to content

Commit

Permalink
Merge pull request #1333 from log4js-node/refactor/LoggingEvent
Browse files Browse the repository at this point in the history
refactor(LoggingEvent): loop through location keys instead of hard-coding one-by-one
  • Loading branch information
lamweili committed Oct 1, 2022
2 parents 570ef53 + 6a8b706 commit cfbc7a0
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 31 deletions.
60 changes: 30 additions & 30 deletions lib/LoggingEvent.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint max-classes-per-file: ["error", 2] */
/* eslint no-underscore-dangle: ["error", { "allow": ["_getLocationKeys"] }] */

const flatted = require('flatted');
const levels = require('./levels');
Expand Down Expand Up @@ -63,18 +64,32 @@ class LoggingEvent {
this.pid = process.pid;
this.error = error;

if (location) {
this.fileName = location.fileName;
this.lineNumber = location.lineNumber;
this.columnNumber = location.columnNumber;
this.callStack = location.callStack;
this.className = location.className;
this.functionName = location.functionName;
this.functionAlias = location.functionAlias;
this.callerName = location.callerName;
if (typeof location !== 'undefined') {
if (!location || typeof location !== 'object' || Array.isArray(location))
throw new TypeError(
'Invalid location type passed to LoggingEvent constructor'
);

this.constructor._getLocationKeys().forEach((key) => {
if (typeof location[key] !== 'undefined') this[key] = location[key];
});
}
}

/** @private */
static _getLocationKeys() {
return [
'fileName',
'lineNumber',
'columnNumber',
'callStack',
'className',
'functionName',
'functionAlias',
'callerName',
];
}

serialise() {
return flatted.stringify(this, (key, value) => {
// JSON.stringify(new Error('test')) returns {}, which is not really useful for us.
Expand Down Expand Up @@ -107,27 +122,12 @@ class LoggingEvent {
}
return serde.deserialise(value);
});
if (
rehydratedEvent.fileName ||
rehydratedEvent.lineNumber ||
rehydratedEvent.columnNumber ||
rehydratedEvent.callStack ||
rehydratedEvent.className ||
rehydratedEvent.functionName ||
rehydratedEvent.functionAlias ||
rehydratedEvent.callerName
) {
rehydratedEvent.location = {
fileName: rehydratedEvent.fileName,
lineNumber: rehydratedEvent.lineNumber,
columnNumber: rehydratedEvent.columnNumber,
callStack: rehydratedEvent.callStack,
className: rehydratedEvent.className,
functionName: rehydratedEvent.functionName,
functionAlias: rehydratedEvent.functionAlias,
callerName: rehydratedEvent.callerName,
};
}
this._getLocationKeys().forEach((key) => {
if (typeof rehydratedEvent[key] !== 'undefined') {
if (!rehydratedEvent.location) rehydratedEvent.location = {};
rehydratedEvent.location[key] = rehydratedEvent[key];
}
});
event = new LoggingEvent(
rehydratedEvent.categoryName,
levels.getLevel(rehydratedEvent.level.levelStr),
Expand Down
15 changes: 15 additions & 0 deletions test/tap/LoggingEvent-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ const LoggingEvent = require('../../lib/LoggingEvent');
const levels = require('../../lib/levels');

test('LoggingEvent', (batch) => {
batch.test('should throw error for invalid location', (t) => {
t.throws(
() =>
new LoggingEvent(
'cheese',
levels.DEBUG,
['log message'],
undefined,
[]
),
'Invalid location type passed to LoggingEvent constructor'
);
t.end();
});

batch.test('should serialise to flatted', (t) => {
const event = new LoggingEvent(
'cheese',
Expand Down
5 changes: 4 additions & 1 deletion test/tap/logger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,10 @@ test('../../lib/logger', (batch) => {
t.equal(events[0].fileName, initialEvent.fileName);
t.equal(events[0].columnNumber, initialEvent.columnNumber);

t.throws(() => logger.setParseCallStackFunction('not a function'));
t.throws(
() => logger.setParseCallStackFunction('not a function'),
'Invalid type passed to setParseCallStackFunction'
);

t.end();
});
Expand Down

0 comments on commit cfbc7a0

Please sign in to comment.