add maxLength to recording

This commit is contained in:
gabriel-cloud 2023-05-19 22:02:24 +00:00
parent 26dcec62f9
commit cd8b019e3f
2 changed files with 5 additions and 1 deletions

View File

@ -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.

View File

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