mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
Merge pull request #913 from rommni/feature/fileNameTruncate
feat: add delimiter for fileName depth to %f layout patern
This commit is contained in:
commit
f428d7d0fd
@ -112,7 +112,8 @@ Fields can be any of:
|
||||
* `%%` % - for when you want a literal `%` in your output
|
||||
* `%n` newline
|
||||
* `%z` process id (from `process.pid`)
|
||||
* `%f` filename (requires `enableCallStack: true` on the category, see [configuration object](api.md))
|
||||
* `%f` full path of filename (requires `enableCallStack: true` on the category, see [configuration object](api.md))
|
||||
* `%f{depth}` path's depth let you chose to have only filename (`%f{1}`) or a chosen number of directories
|
||||
* `%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))
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
const dateFormat = require('date-format');
|
||||
const os = require('os');
|
||||
const util = require('util');
|
||||
const path = require('path');
|
||||
|
||||
const eol = os.EOL || '\n';
|
||||
|
||||
@ -219,8 +220,17 @@ function patternLayout(pattern, tokens) {
|
||||
return null;
|
||||
}
|
||||
|
||||
function fileName(loggingEvent) {
|
||||
return loggingEvent.fileName || '';
|
||||
function fileName(loggingEvent, specifier) {
|
||||
let filename = loggingEvent.fileName || '';
|
||||
if (specifier) {
|
||||
const fileDepth = parseInt(specifier, 10);
|
||||
const fileList = filename.split(path.sep);
|
||||
if (fileList.length > fileDepth) {
|
||||
filename = fileList.slice(-fileDepth).join(path.sep);
|
||||
}
|
||||
}
|
||||
|
||||
return filename;
|
||||
}
|
||||
|
||||
function lineNumber(loggingEvent) {
|
||||
|
||||
@ -306,6 +306,16 @@ test('log4js layouts', (batch) => {
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.test('%f should handle filename depth', (assert) => {
|
||||
testPattern(assert, layout, event, tokens, '%f{1}', 'layouts-test.js');
|
||||
testPattern(assert, layout, event, tokens, '%f{2}', 'tap/layouts-test.js');
|
||||
testPattern(assert, layout, event, tokens, '%f{3}', 'test/tap/layouts-test.js');
|
||||
testPattern(assert, layout, event, tokens, '%f{4}', 'log4js-node/test/tap/layouts-test.js');
|
||||
testPattern(assert, layout, event, tokens, '%f{5}', '/log4js-node/test/tap/layouts-test.js');
|
||||
testPattern(assert, layout, event, tokens, '%f{99}', '/log4js-node/test/tap/layouts-test.js');
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.test('%l should output line number', (assert) => {
|
||||
testPattern(assert, layout, event, tokens, '%l', lineNumber.toString());
|
||||
assert.end();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user