mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
refactor(LoggingEvent): loop through location keys instead of hard-coding one-by-one
This commit is contained in:
parent
570ef530dc
commit
31ae2b258d
@ -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');
|
||||
@ -63,18 +64,27 @@ 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') {
|
||||
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.
|
||||
@ -107,27 +117,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),
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user