mirror of
https://github.com/log4js-node/log4js-node.git
synced 2025-12-08 19:26:01 +00:00
Merge pull request #1386 from gabriel-cloud/gabriel-cloud/issue1385
add maxLength to recording - closes #1385
This commit is contained in:
commit
c5d71e9212
@ -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.
|
||||
|
||||
|
||||
@ -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);
|
||||
};
|
||||
}
|
||||
|
||||
@ -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();
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user