|
1 | 1 | const { test } = require('tap');
|
2 | 2 | const log4js = require('../../lib/log4js');
|
3 | 3 |
|
4 |
| -test('recording appender', (t) => { |
5 |
| - log4js.configure({ |
6 |
| - appenders: { rec: { type: 'recording' } }, |
7 |
| - categories: { default: { appenders: ['rec'], level: 'debug' } }, |
| 4 | +test('recording appender', (batch) => { |
| 5 | + batch.test('should store logs in memory until cleared', (t) => { |
| 6 | + log4js.configure({ |
| 7 | + appenders: { rec: { type: 'recording' } }, |
| 8 | + categories: { default: { appenders: ['rec'], level: 'debug' } }, |
| 9 | + }); |
| 10 | + |
| 11 | + const logger = log4js.getLogger(); |
| 12 | + logger.level = 'debug'; |
| 13 | + logger.debug('This will go to the recording!'); |
| 14 | + logger.debug('Another one'); |
| 15 | + |
| 16 | + const recording = log4js.recording(); |
| 17 | + const loggingEvents = recording.playback(); |
| 18 | + |
| 19 | + t.equal(loggingEvents.length, 2, 'There should be 2 recorded events'); |
| 20 | + t.equal(loggingEvents[0].data[0], 'This will go to the recording!'); |
| 21 | + t.equal(loggingEvents[1].data[0], 'Another one'); |
| 22 | + |
| 23 | + recording.reset(); |
| 24 | + const loggingEventsPostReset = recording.playback(); |
| 25 | + |
| 26 | + t.equal( |
| 27 | + loggingEventsPostReset.length, |
| 28 | + 0, |
| 29 | + 'There should be 0 recorded events' |
| 30 | + ); |
| 31 | + |
| 32 | + t.end(); |
8 | 33 | });
|
9 | 34 |
|
10 |
| - const logger = log4js.getLogger(); |
11 |
| - logger.level = 'debug'; |
12 |
| - logger.debug('This will go to the recording!'); |
13 |
| - logger.debug('Another one'); |
| 35 | + batch.test('should store 2 rolling logs in memory until cleared', (t) => { |
| 36 | + log4js.configure({ |
| 37 | + appenders: { rec2: { type: 'recording', maxLength: 2 } }, |
| 38 | + categories: { default: { appenders: ['rec2'], level: 'debug' } }, |
| 39 | + }); |
14 | 40 |
|
15 |
| - const recording = log4js.recording(); |
16 |
| - const loggingEvents = recording.playback(); |
| 41 | + const logger = log4js.getLogger(); |
| 42 | + logger.level = 'debug'; |
| 43 | + logger.debug('First log entry'); |
| 44 | + logger.debug('Second log entry'); |
17 | 45 |
|
18 |
| - t.equal(loggingEvents.length, 2, 'There should be 2 recorded events'); |
19 |
| - t.equal(loggingEvents[0].data[0], 'This will go to the recording!'); |
20 |
| - t.equal(loggingEvents[1].data[0], 'Another one'); |
| 46 | + const recording = log4js.recording(); |
21 | 47 |
|
22 |
| - recording.reset(); |
23 |
| - const loggingEventsPostReset = recording.playback(); |
| 48 | + t.equal( |
| 49 | + recording.playback().length, |
| 50 | + 2, |
| 51 | + 'There should be 2 recorded events' |
| 52 | + ); |
| 53 | + t.equal(recording.playback()[0].data[0], 'First log entry'); |
| 54 | + t.equal(recording.playback()[1].data[0], 'Second log entry'); |
24 | 55 |
|
25 |
| - t.equal( |
26 |
| - loggingEventsPostReset.length, |
27 |
| - 0, |
28 |
| - 'There should be 0 recorded events' |
29 |
| - ); |
| 56 | + logger.debug('Third log entry'); |
| 57 | + |
| 58 | + t.equal( |
| 59 | + recording.playback().length, |
| 60 | + 2, |
| 61 | + 'There should still be 2 recording events' |
| 62 | + ); |
| 63 | + t.equal(recording.playback()[0].data[0], 'Second log entry'); |
| 64 | + t.equal(recording.playback()[1].data[0], 'Third log entry'); |
| 65 | + |
| 66 | + recording.reset(); |
| 67 | + const loggingEventsPostReset = recording.playback(); |
| 68 | + |
| 69 | + t.equal( |
| 70 | + loggingEventsPostReset.length, |
| 71 | + 0, |
| 72 | + 'There should be 0 recorded events' |
| 73 | + ); |
| 74 | + |
| 75 | + t.end(); |
| 76 | + }); |
30 | 77 |
|
31 |
| - t.end(); |
| 78 | + batch.end(); |
32 | 79 | });
|
0 commit comments