From 0632c2197ec193ca07d52a05946aae114a91e3ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20Lef=C3=A8vre?= Date: Sat, 3 Dec 2022 22:49:28 +0100 Subject: [PATCH] feat issue #1341 (Layouts) : taking into account code review's feedback --- docs/layouts.md | 4 ++-- lib/layouts.js | 11 +++++---- test/tap/layouts-test.js | 49 +++++++++++++++++++--------------------- 3 files changed, 32 insertions(+), 32 deletions(-) diff --git a/docs/layouts.md b/docs/layouts.md index 7b14f27..1e11e2e 100644 --- a/docs/layouts.md +++ b/docs/layouts.md @@ -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 diff --git a/lib/layouts.js b/lib/layouts.js index a969324..13f0cf1 100644 --- a/lib/layouts.js +++ b/lib/layouts.js @@ -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); } diff --git a/test/tap/layouts-test.js b/test/tap/layouts-test.js index a736e5c..2375389 100644 --- a/test/tap/layouts-test.js +++ b/test/tap/layouts-test.js @@ -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 = [