feat issue #1341 (Layouts) : taking into account code review's feedback

This commit is contained in:
Benoît Lefèvre 2022-12-03 22:49:28 +01:00
parent e01c3c7bbf
commit 0632c2197e
3 changed files with 32 additions and 32 deletions

View File

@ -134,8 +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)
- `%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

View File

@ -101,8 +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)
* - %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
@ -213,8 +213,11 @@ function patternLayout(pattern, tokens) {
}
function formatMessage(loggingEvent, specifier) {
const [lowerBound, upperBound] = specifier ? specifier.split(',') : [];
const dataSlice = loggingEvent.data.slice(lowerBound, upperBound);
let dataSlice = loggingEvent.data;
if (specifier) {
const [lowerBound, upperBound] = specifier.split(',');
dataSlice = dataSlice.slice(lowerBound, upperBound);
}
return util.format(...dataSlice);
}

View File

@ -393,28 +393,7 @@ test('log4js layouts', (batch) => {
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) => {
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',
@ -426,13 +405,31 @@ test('log4js layouts', (batch) => {
layout,
eventWithSeveralDataEntry,
tokens,
'%m{0, 1}',
'%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) => {
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',
@ -450,7 +447,7 @@ test('log4js layouts', (batch) => {
assert.end();
});
t.test('%m{1, 2} shoud only consider data.slice( 1, 2 )', (assert) => {
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',
@ -469,7 +466,7 @@ test('log4js layouts', (batch) => {
});
t.test(
'%m{0, -1} should consider the whole data except the last element',
'%m{0,-1} should consider the whole data except the last element',
(assert) => {
const eventWithSeveralDataEntry = JSON.parse(JSON.stringify(event));
eventWithSeveralDataEntry.data = [