feat: allow setting how deeply a logger should search into the stack

`logger.callStackLinesToSkip = 2` will make it skip 2 extra lines above the base.

You can't skip negative lines

Refs: #1268
This commit is contained in:
Zachary Haber 2022-06-17 14:33:51 -05:00 committed by Lam Wei Li
parent 89b8a58f3f
commit a17c8e5e6f
No known key found for this signature in database
GPG Key ID: 90F6ABECF080D7BF

View File

@ -94,6 +94,8 @@ class Logger {
this.category = name;
this.context = {};
/** @private */
this.callStackSkipIndex = 0;
/** @private */
this.parseCallStack = defaultParseCallStack;
debug(`Logger created (${this.category}, ${this.level})`);
}
@ -120,6 +122,20 @@ class Logger {
categories.setEnableCallStackForCategory(this.category, bool === true);
}
get callStackLinesToSkip() {
return this.callStackSkipIndex;
}
set callStackLinesToSkip(number) {
if (typeof number !== 'number') {
throw new TypeError('Must be a number');
}
if (number < 0) {
throw new RangeError('Must be >= 0');
}
this.callStackSkipIndex = number;
}
log(level, ...args) {
const logLevel = levels.getLevel(level);
if (!logLevel) {
@ -150,7 +166,10 @@ class Logger {
if (this.useCallStack) {
try {
if (data[0] instanceof Error) {
callStack = this.parseCallStack(data[0], baseCallStackSkip);
callStack = this.parseCallStack(
data[0],
this.callStackSkipIndex + baseCallStackSkip
);
}
} catch (_err) {
// Ignore Error
@ -159,7 +178,9 @@ class Logger {
callStack ||
this.parseCallStack(
new Error(),
defaultErrorCallStackSkip + baseCallStackSkip
this.callStackSkipIndex +
defaultErrorCallStackSkip +
baseCallStackSkip
);
}
const loggingEvent = new LoggingEvent(