feat: allow to change default way to parse callstack

This commit is contained in:
Romain 2019-07-09 11:54:38 +02:00
parent 0d7fd0cf1d
commit 4f9e2a3b86
2 changed files with 8 additions and 2 deletions

View File

@ -32,6 +32,7 @@ This function takes a single optional string argument to denote the category to
* `addContext(<key>,<value>)` - where `<key>` is a string, `<value>` can be anything. This stores a key-value pair that is added to all log events generated by the logger. Uses would be to add ids for tracking a user through your application. Currently only the `logFaces` appenders make use of the context values.
* `removeContext(<key>)` - removes a previously defined key-value pair from the context.
* `clearContext()` - removes all context pairs from the logger.
* `setParseCallStackFunction(function)` - Allow to override the default way to parse the callstack data for the layout patern, a generic javascript Error object is passed to the function. Must return an object with properties : `functionName` / `fileName` / `lineNumber` / `columnNumber` / `callStack`. Can for exemple be used if all of your log call are made from one "debug" class and you would to "erase" this class from the callstack to only show the function which called your "debug" class.
The `Logger` object has the following property:
* `level` - where `level` is a log4js level or a string that matches a level (e.g. 'info', 'INFO', etc). This allows overriding the configured level for this logger. Changing this value applies to all loggers of the same category.

View File

@ -10,7 +10,7 @@ const categories = require('./categories');
const configuration = require('./configuration');
const stackReg = /at (?:(.+)\s+\()?(?:(.+?):(\d+)(?::(\d+))?|([^)]+))\)?/;
function parseCallStack(data, skipIdx = 4) {
function defautParseCallStack(data, skipIdx = 4) {
const stacklines = data.stack.split('\n').slice(skipIdx);
const lineMatch = stackReg.exec(stacklines[0]);
if (lineMatch && lineMatch.length === 6) {
@ -44,6 +44,7 @@ class Logger {
}
this.category = name;
this.context = {};
this.parseCallStack = defautParseCallStack;
debug(`Logger created (${this.category}, ${this.level})`);
}
@ -81,7 +82,7 @@ class Logger {
level,
data,
this.context,
(this.useCallStack) && parseCallStack(new Error())
(this.useCallStack) && this.parseCallStack(new Error())
);
clustering.send(loggingEvent);
}
@ -97,6 +98,10 @@ class Logger {
clearContext() {
this.context = {};
}
setParseCallStackFunction(parseFunction) {
this.parseCallStack = parseFunction;
}
}
function addLevelMethods(target) {