Merge pull request #913 from rommni/feature/fileNameTruncate

feat: add delimiter for fileName depth to %f layout patern
This commit is contained in:
Gareth Jones 2019-07-10 08:18:21 +10:00 committed by GitHub
commit f428d7d0fd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -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))

View File

@ -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) {

View File

@ -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();