From cd8b019e3f09c7d13cf2aafc31951d86e8f3a4f4 Mon Sep 17 00:00:00 2001 From: gabriel-cloud <54978752+gabriel-cloud@users.noreply.github.com> Date: Fri, 19 May 2023 22:02:24 +0000 Subject: [PATCH 1/2] add maxLength to recording --- docs/recording.md | 1 + lib/appenders/recording.js | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/docs/recording.md b/docs/recording.md index b9e8cc0..ecdafb4 100644 --- a/docs/recording.md +++ b/docs/recording.md @@ -5,6 +5,7 @@ This appender stores the log events in memory. It is mainly useful for testing ( ## Configuration - `type` - `recording` +- `maxLength` - `integer` (optional, defaults to undefined) - the maximum array length for the recording. If not specified, the array will grow until cleared There is no other configuration for this appender. diff --git a/lib/appenders/recording.js b/lib/appenders/recording.js index ca90e07..ec91713 100644 --- a/lib/appenders/recording.js +++ b/lib/appenders/recording.js @@ -2,12 +2,15 @@ const debug = require('debug')('log4js:recording'); const recordedEvents = []; -function configure() { +function configure(config) { return function (logEvent) { debug( `received logEvent, number of events now ${recordedEvents.length + 1}` ); debug('log event was ', logEvent); + if (config.maxLength && recordedEvents.length >= config.maxLength) { + recordedEvents.shift(); + } recordedEvents.push(logEvent); }; } From 4dc786eaeb1fb8959450961eb72fafc7a8ac9450 Mon Sep 17 00:00:00 2001 From: gabriel-cloud <54978752+gabriel-cloud@users.noreply.github.com> Date: Sat, 20 May 2023 20:39:36 +0000 Subject: [PATCH 2/2] add maxLength to testCase --- test/tap/recordingAppender-test.js | 89 +++++++++++++++++++++++------- 1 file changed, 68 insertions(+), 21 deletions(-) diff --git a/test/tap/recordingAppender-test.js b/test/tap/recordingAppender-test.js index c8066f8..574c08f 100644 --- a/test/tap/recordingAppender-test.js +++ b/test/tap/recordingAppender-test.js @@ -1,32 +1,79 @@ const { test } = require('tap'); const log4js = require('../../lib/log4js'); -test('recording appender', (t) => { - log4js.configure({ - appenders: { rec: { type: 'recording' } }, - categories: { default: { appenders: ['rec'], level: 'debug' } }, +test('recording appender', (batch) => { + batch.test('should store logs in memory until cleared', (t) => { + log4js.configure({ + appenders: { rec: { type: 'recording' } }, + categories: { default: { appenders: ['rec'], level: 'debug' } }, + }); + + const logger = log4js.getLogger(); + logger.level = 'debug'; + logger.debug('This will go to the recording!'); + logger.debug('Another one'); + + const recording = log4js.recording(); + const loggingEvents = recording.playback(); + + t.equal(loggingEvents.length, 2, 'There should be 2 recorded events'); + t.equal(loggingEvents[0].data[0], 'This will go to the recording!'); + t.equal(loggingEvents[1].data[0], 'Another one'); + + recording.reset(); + const loggingEventsPostReset = recording.playback(); + + t.equal( + loggingEventsPostReset.length, + 0, + 'There should be 0 recorded events' + ); + + t.end(); }); - const logger = log4js.getLogger(); - logger.level = 'debug'; - logger.debug('This will go to the recording!'); - logger.debug('Another one'); + batch.test('should store 2 rolling logs in memory until cleared', (t) => { + log4js.configure({ + appenders: { rec2: { type: 'recording', maxLength: 2 } }, + categories: { default: { appenders: ['rec2'], level: 'debug' } }, + }); - const recording = log4js.recording(); - const loggingEvents = recording.playback(); + const logger = log4js.getLogger(); + logger.level = 'debug'; + logger.debug('First log entry'); + logger.debug('Second log entry'); - t.equal(loggingEvents.length, 2, 'There should be 2 recorded events'); - t.equal(loggingEvents[0].data[0], 'This will go to the recording!'); - t.equal(loggingEvents[1].data[0], 'Another one'); + const recording = log4js.recording(); - recording.reset(); - const loggingEventsPostReset = recording.playback(); + t.equal( + recording.playback().length, + 2, + 'There should be 2 recorded events' + ); + t.equal(recording.playback()[0].data[0], 'First log entry'); + t.equal(recording.playback()[1].data[0], 'Second log entry'); - t.equal( - loggingEventsPostReset.length, - 0, - 'There should be 0 recorded events' - ); + logger.debug('Third log entry'); - t.end(); + t.equal( + recording.playback().length, + 2, + 'There should still be 2 recording events' + ); + t.equal(recording.playback()[0].data[0], 'Second log entry'); + t.equal(recording.playback()[1].data[0], 'Third log entry'); + + recording.reset(); + const loggingEventsPostReset = recording.playback(); + + t.equal( + loggingEventsPostReset.length, + 0, + 'There should be 0 recorded events' + ); + + t.end(); + }); + + batch.end(); });