feat issue #1341 (Layouts) : support a specifier on %m

This commit is contained in:
Benoît Lefèvre 2022-11-14 02:13:14 +01:00
parent b2c97040fb
commit aedef32bb9
2 changed files with 128 additions and 2 deletions

View File

@ -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 ans u are integers : log data.slice(l,u)
* - %d date in constious formats
* - %% %
* - %n newline
@ -137,6 +139,8 @@ function patternLayout(pattern, tokens) {
const regex =
/%(-?[0-9]+)?(\.?-?[0-9]+)?([[\]cdhmnprzxXyflosCMAF%])(\{([^}]+)\})?|([^%]+)/;
const mSpecifierRegex = /\s*(-?\d)(\s*,\s*(-?\d))?\w*/;
pattern = pattern || TTCC_CONVERSION_PATTERN;
function categoryName(loggingEvent, specifier) {
@ -210,8 +214,14 @@ function patternLayout(pattern, tokens) {
return os.hostname().toString();
}
function formatMessage(loggingEvent) {
return util.format(...loggingEvent.data);
function formatMessage(loggingEvent, specifier) {
const match = mSpecifierRegex.exec(specifier);
const lowerBound = match ? parseInt(match[1], 10) : 0;
const upperBound = match && match[3] ? parseInt(match[3], 10) : undefined;
const dataSlice = loggingEvent.data.slice(lowerBound, upperBound);
return util.format(...dataSlice);
}
function endOfLine() {

View File

@ -375,6 +375,122 @@ 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{P}, with P been an integer, shoud only consider data.slice( P )',
(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} shoud 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{1, 2} shoud 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();