2021-04-23 15:31:12 +02:00
|
|
|
import { configure, getLogger } from 'log4js';
|
2016-10-26 10:43:11 +02:00
|
|
|
|
2021-03-23 08:46:01 +01:00
|
|
|
export type LogProvider = (category?: string) => Logger;
|
2014-10-20 13:03:43 +02:00
|
|
|
|
2021-04-23 15:31:12 +02:00
|
|
|
export enum LogLevel {
|
|
|
|
debug = 'debug',
|
|
|
|
info = 'info',
|
|
|
|
warn = 'warn',
|
|
|
|
error = 'error',
|
|
|
|
fatal = 'fatal',
|
|
|
|
}
|
|
|
|
|
2021-03-23 08:46:01 +01:00
|
|
|
export interface Logger {
|
|
|
|
debug(message: any, ...args: any[]): void;
|
|
|
|
info(message: any, ...args: any[]): void;
|
|
|
|
warn(message: any, ...args: any[]): void;
|
|
|
|
error(message: any, ...args: any[]): void;
|
|
|
|
fatal(message: any, ...args: any[]): void;
|
|
|
|
}
|
|
|
|
|
2021-04-23 15:31:12 +02:00
|
|
|
export function getDefaultLogProvider(
|
|
|
|
logLevel: LogLevel = LogLevel.error,
|
|
|
|
): LogProvider {
|
2021-03-23 08:46:01 +01:00
|
|
|
configure({
|
2017-11-11 11:13:34 +01:00
|
|
|
appenders: {
|
|
|
|
console: { type: 'console' },
|
|
|
|
},
|
|
|
|
categories: {
|
2021-04-23 15:31:12 +02:00
|
|
|
default: { appenders: ['console'], level: logLevel },
|
2017-08-04 16:03:15 +02:00
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2021-03-23 08:46:01 +01:00
|
|
|
return getLogger;
|
2017-08-04 16:03:15 +02:00
|
|
|
}
|
2017-08-05 15:38:55 +02:00
|
|
|
|
2021-03-23 08:46:01 +01:00
|
|
|
function validate(isValid: boolean, msg: string) {
|
2017-08-05 15:38:55 +02:00
|
|
|
if (!isValid) {
|
|
|
|
throw new TypeError(msg);
|
|
|
|
}
|
|
|
|
}
|
2020-04-14 22:29:11 +02:00
|
|
|
|
2021-03-23 08:46:01 +01:00
|
|
|
export function validateLogProvider(provider: LogProvider): void {
|
2020-04-14 22:29:11 +02:00
|
|
|
validate(typeof provider === 'function', 'Provider needs to be a function');
|
|
|
|
|
|
|
|
const logger = provider('unleash:logger');
|
|
|
|
validate(typeof logger.debug === 'function', 'Logger must implement debug');
|
|
|
|
validate(typeof logger.info === 'function', 'Logger must implement info');
|
|
|
|
validate(typeof logger.warn === 'function', 'Logger must implement warn');
|
|
|
|
validate(typeof logger.error === 'function', 'Logger must implement error');
|
|
|
|
}
|