mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
Merge pull request #1346 from Mumeii/feature/issue-1341
feat issue #1341 (Layouts) : support a specifier on %m
This commit is contained in:
commit
ee0f4e46e2
@ -134,6 +134,8 @@ Fields can be any of:
|
||||
- `%c` log category
|
||||
- `%h` hostname
|
||||
- `%m` log data
|
||||
- `%m{l}` where l is an integer, log data.slice(l)
|
||||
- `%m{l,u}` where l and u are integers, log data.slice(l, u)
|
||||
- `%d` date, formatted - default is `ISO8601`, format options are: `ISO8601`, `ISO8601_WITH_TZ_OFFSET`, `ABSOLUTETIME`, `DATETIME`, or any string compatible with the [date-format](https://www.npmjs.com/package/date-format) library. e.g. `%d{DATETIME}`, `%d{yyyy/MM/dd-hh.mm.ss}`
|
||||
- `%%` % - for when you want a literal `%` in your output
|
||||
- `%n` newline
|
||||
|
||||
@ -101,6 +101,8 @@ function dummyLayout(loggingEvent) {
|
||||
* - %c log category
|
||||
* - %h hostname
|
||||
* - %m log data
|
||||
* - %m{l} where l is an integer, log data.slice(l)
|
||||
* - %m{l,u} where l and u are integers, log data.slice(l, u)
|
||||
* - %d date in constious formats
|
||||
* - %% %
|
||||
* - %n newline
|
||||
@ -210,8 +212,13 @@ function patternLayout(pattern, tokens) {
|
||||
return os.hostname().toString();
|
||||
}
|
||||
|
||||
function formatMessage(loggingEvent) {
|
||||
return util.format(...loggingEvent.data);
|
||||
function formatMessage(loggingEvent, specifier) {
|
||||
let dataSlice = loggingEvent.data;
|
||||
if (specifier) {
|
||||
const [lowerBound, upperBound] = specifier.split(',');
|
||||
dataSlice = dataSlice.slice(lowerBound, upperBound);
|
||||
}
|
||||
return util.format(...dataSlice);
|
||||
}
|
||||
|
||||
function endOfLine() {
|
||||
|
||||
@ -375,6 +375,101 @@ test('log4js layouts', (batch) => {
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.test('%m should apply util.format on data', (assert) => {
|
||||
const eventWithSeveralDataEntry = JSON.parse(JSON.stringify(event));
|
||||
eventWithSeveralDataEntry.data = [
|
||||
'This %s a %s like other ones',
|
||||
"isn't",
|
||||
'test',
|
||||
];
|
||||
testPattern(
|
||||
assert,
|
||||
layout,
|
||||
eventWithSeveralDataEntry,
|
||||
tokens,
|
||||
'%m',
|
||||
"This isn't a test like other ones"
|
||||
);
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.test('%m{1} should only consider data.slice(1)', (assert) => {
|
||||
const eventWithSeveralDataEntry = JSON.parse(JSON.stringify(event));
|
||||
eventWithSeveralDataEntry.data = [
|
||||
'This %s a %s like other ones',
|
||||
"isn't",
|
||||
'test',
|
||||
];
|
||||
testPattern(
|
||||
assert,
|
||||
layout,
|
||||
eventWithSeveralDataEntry,
|
||||
tokens,
|
||||
'%m{1}',
|
||||
"isn't test"
|
||||
);
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.test('%m{0,1} should behave like a dummy layout', (assert) => {
|
||||
const eventWithSeveralDataEntry = JSON.parse(JSON.stringify(event));
|
||||
eventWithSeveralDataEntry.data = [
|
||||
'This %s a %s like other ones',
|
||||
"isn't",
|
||||
'test',
|
||||
];
|
||||
testPattern(
|
||||
assert,
|
||||
layout,
|
||||
eventWithSeveralDataEntry,
|
||||
tokens,
|
||||
'%m{0,1}',
|
||||
'This %s a %s like other ones'
|
||||
);
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.test('%m{1,2} should only consider data.slice(1, 2)', (assert) => {
|
||||
const eventWithSeveralDataEntry = JSON.parse(JSON.stringify(event));
|
||||
eventWithSeveralDataEntry.data = [
|
||||
'This %s a %s like other ones',
|
||||
"isn't",
|
||||
'test',
|
||||
];
|
||||
testPattern(
|
||||
assert,
|
||||
layout,
|
||||
eventWithSeveralDataEntry,
|
||||
tokens,
|
||||
'%m{1,2}',
|
||||
"isn't"
|
||||
);
|
||||
assert.end();
|
||||
});
|
||||
|
||||
t.test(
|
||||
'%m{0,-1} should consider the whole data except the last element',
|
||||
(assert) => {
|
||||
const eventWithSeveralDataEntry = JSON.parse(JSON.stringify(event));
|
||||
eventWithSeveralDataEntry.data = [
|
||||
'This %s a %s like %s ones',
|
||||
"isn't",
|
||||
'test',
|
||||
'other',
|
||||
"won't be considered in call to util.format",
|
||||
];
|
||||
testPattern(
|
||||
assert,
|
||||
layout,
|
||||
eventWithSeveralDataEntry,
|
||||
tokens,
|
||||
'%m{0,-1}',
|
||||
"This isn't a test like other ones"
|
||||
);
|
||||
assert.end();
|
||||
}
|
||||
);
|
||||
|
||||
t.test('%n should output a new line', (assert) => {
|
||||
testPattern(assert, layout, event, tokens, '%n', EOL);
|
||||
assert.end();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user