Merge pull request #914 from rommni/feature/setParseCallStack

feat: allow to change default way to parse callstack
This commit is contained in:
Gareth Jones 2019-07-10 08:14:27 +10:00 committed by GitHub
commit 1cc724d27d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 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) {

View File

@ -218,5 +218,32 @@ test('../../lib/logger', (batch) => {
t.end();
});
batch.test('should correctly change the parseCallStack function', (t) => {
const logger = new Logger('stack');
const parseFunction = function () {
return {
functionName: 'test function name',
fileName: 'test file name',
lineNumber: 15,
columnNumber: 25,
callStack: 'test callstack',
};
};
logger.level = 'debug';
logger.useCallStack = true;
logger.setParseCallStackFunction(parseFunction);
t.equal(logger.parseCallStack, parseFunction);
logger.info('test parseCallStack');
t.equal(events[0].functionName, 'test function name');
t.equal(events[0].fileName, 'test file name');
t.equal(events[0].lineNumber, 15);
t.equal(events[0].columnNumber, 25);
t.equal(events[0].callStack, 'test callstack');
t.end();
});
batch.end();
});