Merge pull request #1317 from log4js-node/refactor/#1316

refactor(#1316): code flow and readability (%C:class, %M:function, %A:alias, %F:full - in order)
This commit is contained in:
Lam Wei Li 2022-09-01 22:28:14 +08:00 committed by GitHub
commit accaef82a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 22 additions and 22 deletions

View File

@ -143,10 +143,10 @@ Fields can be any of:
- `%l` line number (requires `enableCallStack: true` on the category, see [configuration object](api.md))
- `%o` column postion (requires `enableCallStack: true` on the category, see [configuration object](api.md))
- `%s` call stack (requires `enableCallStack: true` on the category, see [configuration object](api.md))
- `%M` function or method name (requires `enableCallStack: true` on the category, see [configuration object](api.md))
- `%C` class name (requires `enableCallStack: true` on the category, see [configuration object](api.md))
- `%A` function or method name alias (requires `enableCallStack: true` on the category, see [configuration object](api.md))
- `%F` fully qualified caller name (requires `enableCallStack: true` on the category, see [configuration object](api.md))
- `%C` class name (requires `enableCallStack: true` on the category, see [configuration object](api.md) and [#1316](https://github.com/log4js-node/log4js-node/pull/1316))
- `%M` method or function name (requires `enableCallStack: true` on the category, see [configuration object](api.md) and [#1316](https://github.com/log4js-node/log4js-node/pull/1316))
- `%A` method or function alias (requires `enableCallStack: true` on the category, see [configuration object](api.md) and [#1316](https://github.com/log4js-node/log4js-node/pull/1316))
- `%F` fully qualified caller name (requires `enableCallStack: true` on the category, see [configuration object](api.md) and [#1316](https://github.com/log4js-node/log4js-node/pull/1316))
- `%x{<tokenname>}` add dynamic tokens to your log. Tokens are specified in the tokens parameter.
- `%X{<tokenname>}` add values from the Logger context. Tokens are keys into the context values.
- `%[` start a coloured block (colour will be taken from the log level, similar to `colouredLayout`)

View File

@ -23,12 +23,12 @@ class LoggingEvent {
this.pid = process.pid;
if (location) {
this.functionName = location.functionName;
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;
}
@ -77,12 +77,12 @@ class LoggingEvent {
return value;
});
rehydratedEvent.location = {
functionName: rehydratedEvent.functionName,
fileName: rehydratedEvent.fileName,
lineNumber: rehydratedEvent.lineNumber,
columnNumber: rehydratedEvent.columnNumber,
callStack: rehydratedEvent.callStack,
className: rehydratedEvent.className,
functionName: rehydratedEvent.functionName,
functionAlias: rehydratedEvent.functionAlias,
callerName: rehydratedEvent.callerName,
};

View File

@ -109,10 +109,10 @@ function dummyLayout(loggingEvent) {
* - %l line number
* - %o column postion
* - %s call stack
* - %M method or function name
* - %C class name
* - %A method or function name
* - %F fully qualified caller name
* - %C class name [#1316](https://github.com/log4js-node/log4js-node/pull/1316)
* - %M method or function name [#1316](https://github.com/log4js-node/log4js-node/pull/1316)
* - %A method or function alias [#1316](https://github.com/log4js-node/log4js-node/pull/1316)
* - %F fully qualified caller name [#1316](https://github.com/log4js-node/log4js-node/pull/1316)
* - %x{<tokenname>} add dynamic tokens to your log. Tokens are specified in the tokens parameter
* - %X{<tokenname>} add dynamic tokens to your log. Tokens are specified in logger context
* You can use %[ and %] to define a colored block.
@ -134,7 +134,7 @@ function dummyLayout(loggingEvent) {
*/
function patternLayout(pattern, tokens) {
const TTCC_CONVERSION_PATTERN = '%r %p %c - %m%n';
const regex = /%(-?[0-9]+)?(\.?-?[0-9]+)?([[\]cdhmnprzxXyflosMCAF%])(\{([^}]+)\})?|([^%]+)/;
const regex = /%(-?[0-9]+)?(\.?-?[0-9]+)?([[\]cdhmnprzxXyflosCMAF%])(\{([^}]+)\})?|([^%]+)/;
pattern = pattern || TTCC_CONVERSION_PATTERN;
@ -326,14 +326,14 @@ function patternLayout(pattern, tokens) {
return loggingEvent.callStack || '';
}
function functionName(loggingEvent) {
return loggingEvent.functionName || '';
}
function className(loggingEvent) {
return loggingEvent.className || '';
}
function functionName(loggingEvent) {
return loggingEvent.functionName || '';
}
function functionAlias(loggingEvent) {
return loggingEvent.functionAlias || '';
}
@ -361,8 +361,8 @@ function patternLayout(pattern, tokens) {
l: lineNumber,
o: columnNumber,
s: callStack,
M: functionName,
C: className,
M: functionName,
A: functionAlias,
F: callerName,
};

View File

@ -31,12 +31,12 @@ function defaultParseCallStack(data, skipIdx = 4) {
}
return {
functionName,
fileName: lineMatch[2],
lineNumber: parseInt(lineMatch[3], 10),
columnNumber: parseInt(lineMatch[4], 10),
callStack: stacklines.join('\n'),
className,
functionName,
functionAlias,
callerName: lineMatch[1] || '',
};

View File

@ -74,12 +74,12 @@ test('LoggingEvent', (batch) => {
const functionAlias = '';
const callerName = '';
const location = {
functionName,
fileName,
lineNumber,
columnNumber,
callStack,
className,
functionName,
functionAlias,
callerName,
};
@ -90,12 +90,12 @@ test('LoggingEvent', (batch) => {
{ user: 'bob' },
location
);
t.equal(event.functionName, functionName);
t.equal(event.fileName, fileName);
t.equal(event.lineNumber, lineNumber);
t.equal(event.columnNumber, columnNumber);
t.equal(event.callStack, callStack);
t.equal(event.className, className);
t.equal(event.functionName, functionName);
t.equal(event.functionAlias, functionAlias);
t.equal(event.callerName, callerName);
@ -106,8 +106,8 @@ test('LoggingEvent', (batch) => {
t.equal(event2.lineNumber, undefined);
t.equal(event2.columnNumber, undefined);
t.equal(event2.callStack, undefined);
t.equal(event2.functionName, undefined);
t.equal(event2.className, undefined);
t.equal(event2.functionName, undefined);
t.equal(event2.functionAlias, undefined);
t.equal(event2.callerName, undefined);
t.end();
@ -125,12 +125,12 @@ test('LoggingEvent', (batch) => {
const functionAlias = 'baz';
const callerName = 'Foo.bar [as baz]';
const location = {
functionName,
fileName,
lineNumber,
columnNumber,
callStack,
className,
functionName,
functionAlias,
callerName,
};
@ -141,12 +141,12 @@ test('LoggingEvent', (batch) => {
{ user: 'bob' },
location
);
t.equal(event.functionName, functionName);
t.equal(event.fileName, fileName);
t.equal(event.lineNumber, lineNumber);
t.equal(event.columnNumber, columnNumber);
t.equal(event.callStack, callStack);
t.equal(event.className, className);
t.equal(event.functionName, functionName);
t.equal(event.functionAlias, functionAlias);
t.equal(event.callerName, callerName);
t.end();