1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-11-01 19:07:38 +01:00
unleash.unleash/src/lib/logger.ts

51 lines
1.4 KiB
TypeScript
Raw Normal View History

2021-04-23 15:31:12 +02:00
import { configure, getLogger } from 'log4js';
2016-10-26 10:43:11 +02:00
export type LogProvider = (category?: string) => Logger;
2021-04-23 15:31:12 +02:00
export enum LogLevel {
debug = 'debug',
info = 'info',
warn = 'warn',
error = 'error',
fatal = 'fatal',
}
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 {
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 },
},
});
return getLogger;
}
function validate(isValid: boolean, msg: string) {
if (!isValid) {
throw new TypeError(msg);
}
}
export function validateLogProvider(provider: LogProvider): void {
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');
}