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] 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); }; }