Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose recording in typescript #1103

Merged
merged 2 commits into from Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -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.
Expand Down Expand Up @@ -150,7 +155,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 @@ -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;

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}`)
}