From 5b320e3b2f6a8c2df16a392030e9dc38343b0415 Mon Sep 17 00:00:00 2001 From: Angelo Polo Date: Thu, 11 Nov 2021 10:51:57 +0100 Subject: [PATCH 1/2] fix: Expose recording in typescript types --- lib/log4js.js | 8 +++++++- types/log4js.d.ts | 10 ++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/lib/log4js.js b/lib/log4js.js index fdd48c84..0934be76 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; @@ -73,6 +74,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. @@ -150,7 +155,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 2fdc99ce..e7e2bc2b 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; @@ -283,6 +285,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; From c5ea84764d52d569dea2bd58f94e5c3451a525c9 Mon Sep 17 00:00:00 2001 From: Angelo Polo Date: Thu, 11 Nov 2021 11:50:24 +0100 Subject: [PATCH 2/2] test: Add typescript recording usage example --- types/test.ts | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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}`) +}