diff --git a/lib/log4js.js b/lib/log4js.js index b399816c..aef83c52 100644 --- a/lib/log4js.js +++ b/lib/log4js.js @@ -29,6 +29,7 @@ const categories = require("./categories"); const Logger = require("./logger"); const clustering = require("./clustering"); const connectLogger = require("./connect-logger"); +const recordingModule = require("./appenders/recording"); let enabled = false; @@ -78,6 +79,10 @@ function configure(configurationFileOrObject) { return log4js; } +function recording() { + return recordingModule +} + /** * Shutdown all log appenders. This will first disable all writing to appenders * and then call the shutdown function each appender. @@ -159,7 +164,8 @@ const log4js = { shutdown, connectLogger, levels, - addLayout: layouts.addLayout + addLayout: layouts.addLayout, + recording, }; module.exports = log4js; diff --git a/types/log4js.d.ts b/types/log4js.d.ts index 6fe4c33d..b7946da2 100644 --- a/types/log4js.d.ts +++ b/types/log4js.d.ts @@ -21,6 +21,8 @@ export function addLayout(name: string, config: (a: any) => (logEvent: LoggingEv export function connectLogger(logger: Logger, options: { format?: Format; level?: string; nolog?: any; statusRules?: any[], context?: boolean }): any; // express.Handler; +export function recording(): Recording; + export const levels: Levels; export function shutdown(cb?: (error: Error) => void): void | null; @@ -332,6 +334,14 @@ export interface Configuration { disableClustering?: boolean; } +export interface Recording { + configure(loggingEvent: LoggingEvent): void + replay(): LoggingEvent[] + playback(): LoggingEvent[] + reset(): void + erase(): void +} + export class Logger { new(dispatch: Function, name: string): Logger; diff --git a/types/test.ts b/types/test.ts index a78f8322..a2f0fafc 100644 --- a/types/test.ts +++ b/types/test.ts @@ -136,3 +136,25 @@ log4js.configure({ appenders: { thing: { type: { configure: () => {} }}}, categories: { default: { appenders: ['thing'], level: 'debug'}} }); + +log4js.configure({ + appenders: { rec: { type: 'recording' } }, + categories: { default: { appenders: [ 'rec'], 'level': 'debug' } } +}); +const logger8 = log4js.getLogger(); +logger8.level = 'debug' +logger8.debug('This will go to the recording!') +logger8.debug('Another one') +const recording = log4js.recording() +const loggingEvents = recording.playback() +if (loggingEvents.length !== 2) { + throw new Error(`Expected 2 recorded events, got ${loggingEvents.length}`) +} +if (loggingEvents[1].data[0] !== 'Another one') { + throw new Error(`Expected message 'Another one', got ${loggingEvents[1].data[0]}`) +} +recording.reset() +const loggingEventsPostReset = recording.playback() +if (loggingEventsPostReset.length !== 0) { + throw new Error(`Expected 0 recorded events after reset, got ${loggingEventsPostReset.length}`) +}