44 lines
1.1 KiB
TypeScript
44 lines
1.1 KiB
TypeScript
export type LoggerLevel = 'none' | 'info' | 'warn' | 'error' | 'debug';
|
|
type LoggerFunction = (...args: any[]) => void;
|
|
|
|
const LOG_LEVELS = ['none', 'info', 'warn', 'error', 'debug'];
|
|
|
|
interface LoggerType {
|
|
info: LoggerFunction;
|
|
warn: LoggerFunction;
|
|
error: LoggerFunction;
|
|
debug: LoggerFunction;
|
|
}
|
|
|
|
export class OAuth2Logger {
|
|
constructor(
|
|
public logLevel: LoggerLevel,
|
|
public logger: LoggerType | undefined = console
|
|
) {}
|
|
|
|
public setLogLevel(logLevel: LoggerLevel): void {
|
|
this.logLevel = logLevel;
|
|
}
|
|
|
|
public setLogger(logger?: LoggerType): void {
|
|
this.logger = logger;
|
|
}
|
|
|
|
public log(level: LoggerLevel, ...args: any[]): void {
|
|
if (!this.logger || this.logLevel === 'none') {
|
|
return;
|
|
}
|
|
|
|
if (LOG_LEVELS.indexOf(this.logLevel) < LOG_LEVELS.indexOf(level)) {
|
|
return;
|
|
}
|
|
|
|
this.logger[level as 'info' | 'warn' | 'error' | 'debug'](...args);
|
|
}
|
|
|
|
public info = this.log.bind(this, 'info');
|
|
public warn = this.log.bind(this, 'warn');
|
|
public error = this.log.bind(this, 'error');
|
|
public debug = this.log.bind(this, 'debug');
|
|
}
|