Skip to content

Commit

Permalink
Merge pull request #1103 from polo-language/recording-typescript
Browse files Browse the repository at this point in the history
Expose recording in typescript
  • Loading branch information
lamweili committed Jan 18, 2022
2 parents 653a20f + c5ea847 commit 51ac865
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/log4js.js
Expand Up @@ -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;

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -159,7 +164,8 @@ const log4js = {
shutdown,
connectLogger,
levels,
addLayout: layouts.addLayout
addLayout: layouts.addLayout,
recording,
};

module.exports = log4js;
10 changes: 10 additions & 0 deletions types/log4js.d.ts
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
22 changes: 22 additions & 0 deletions types/test.ts
Expand Up @@ -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}`)
}

0 comments on commit 51ac865

Please sign in to comment.