2018-12-19 13:16:45 -05:00
|
|
|
import { Logger } from "./logger.ts";
|
2019-01-06 14:19:15 -05:00
|
|
|
import {
|
|
|
|
BaseHandler,
|
|
|
|
ConsoleHandler,
|
|
|
|
WriterHandler,
|
|
|
|
FileHandler
|
|
|
|
} from "./handlers.ts";
|
2018-12-19 13:16:45 -05:00
|
|
|
|
|
|
|
export class LoggerConfig {
|
|
|
|
level?: string;
|
|
|
|
handlers?: string[];
|
|
|
|
}
|
|
|
|
|
2019-01-02 09:12:48 -05:00
|
|
|
export interface LogConfig {
|
2018-12-19 13:16:45 -05:00
|
|
|
handlers?: {
|
2019-01-02 09:12:48 -05:00
|
|
|
[name: string]: BaseHandler;
|
2018-12-19 13:16:45 -05:00
|
|
|
};
|
|
|
|
loggers?: {
|
|
|
|
[name: string]: LoggerConfig;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
const DEFAULT_LEVEL = "INFO";
|
2019-01-02 09:12:48 -05:00
|
|
|
const DEFAULT_CONFIG: LogConfig = {
|
2019-01-27 10:21:00 -05:00
|
|
|
handlers: {
|
|
|
|
"default": new ConsoleHandler(DEFAULT_LEVEL)
|
|
|
|
},
|
2018-12-19 13:16:45 -05:00
|
|
|
|
|
|
|
loggers: {
|
2019-01-27 10:21:00 -05:00
|
|
|
"default": {
|
|
|
|
level: DEFAULT_LEVEL,
|
|
|
|
handlers: ["default"]
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const state = {
|
2019-01-02 09:12:48 -05:00
|
|
|
handlers: new Map(),
|
2018-12-19 13:16:45 -05:00
|
|
|
loggers: new Map(),
|
2019-01-06 14:19:15 -05:00
|
|
|
config: DEFAULT_CONFIG
|
2018-12-19 13:16:45 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
export const handlers = {
|
2019-01-02 09:12:48 -05:00
|
|
|
BaseHandler,
|
|
|
|
ConsoleHandler,
|
|
|
|
WriterHandler,
|
2019-01-06 14:19:15 -05:00
|
|
|
FileHandler
|
2018-12-19 13:16:45 -05:00
|
|
|
};
|
|
|
|
|
2019-01-06 14:19:15 -05:00
|
|
|
export const debug = (msg: string, ...args: any[]) =>
|
2019-01-27 10:21:00 -05:00
|
|
|
getLogger('default').debug(msg, ...args);
|
2019-01-06 14:19:15 -05:00
|
|
|
export const info = (msg: string, ...args: any[]) =>
|
2019-01-27 10:21:00 -05:00
|
|
|
getLogger('default').info(msg, ...args);
|
2019-01-06 14:19:15 -05:00
|
|
|
export const warning = (msg: string, ...args: any[]) =>
|
2019-01-27 10:21:00 -05:00
|
|
|
getLogger('default').warning(msg, ...args);
|
2019-01-06 14:19:15 -05:00
|
|
|
export const error = (msg: string, ...args: any[]) =>
|
2019-01-27 10:21:00 -05:00
|
|
|
getLogger('default').error(msg, ...args);
|
2019-01-06 14:19:15 -05:00
|
|
|
export const critical = (msg: string, ...args: any[]) =>
|
2019-01-27 10:21:00 -05:00
|
|
|
getLogger('default').critical(msg, ...args);
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2018-12-19 13:16:45 -05:00
|
|
|
export function getLogger(name?: string) {
|
|
|
|
if (!name) {
|
2019-01-27 10:21:00 -05:00
|
|
|
return state.loggers.get('default');
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!state.loggers.has(name)) {
|
2019-01-02 09:12:48 -05:00
|
|
|
const logger = new Logger("NOTSET", []);
|
|
|
|
state.loggers.set(name, logger);
|
|
|
|
return logger;
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return state.loggers.get(name);
|
|
|
|
}
|
|
|
|
|
2019-01-19 13:46:46 -05:00
|
|
|
export function getHandler(name: string) {
|
|
|
|
return state.handlers.get(name);
|
|
|
|
}
|
|
|
|
|
2019-01-02 09:12:48 -05:00
|
|
|
export async function setup(config: LogConfig) {
|
2019-01-27 10:21:00 -05:00
|
|
|
state.config = {
|
|
|
|
handlers: {...DEFAULT_CONFIG.handlers, ...config.handlers},
|
|
|
|
loggers: {...DEFAULT_CONFIG.loggers, ...config.loggers}
|
|
|
|
};
|
2019-01-02 09:12:48 -05:00
|
|
|
|
|
|
|
// tear down existing handlers
|
|
|
|
state.handlers.forEach(handler => {
|
|
|
|
handler.destroy();
|
|
|
|
});
|
|
|
|
state.handlers.clear();
|
|
|
|
|
|
|
|
// setup handlers
|
|
|
|
const handlers = state.config.handlers || {};
|
|
|
|
|
|
|
|
for (const handlerName in handlers) {
|
|
|
|
const handler = handlers[handlerName];
|
|
|
|
await handler.setup();
|
|
|
|
state.handlers.set(handlerName, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove existing loggers
|
|
|
|
state.loggers.clear();
|
|
|
|
|
|
|
|
// setup loggers
|
|
|
|
const loggers = state.config.loggers || {};
|
|
|
|
for (const loggerName in loggers) {
|
|
|
|
const loggerConfig = loggers[loggerName];
|
|
|
|
const handlerNames = loggerConfig.handlers || [];
|
|
|
|
const handlers = [];
|
|
|
|
|
|
|
|
handlerNames.forEach(handlerName => {
|
|
|
|
if (state.handlers.has(handlerName)) {
|
|
|
|
handlers.push(state.handlers.get(handlerName));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
const levelName = loggerConfig.level || DEFAULT_LEVEL;
|
|
|
|
const logger = new Logger(levelName, handlers);
|
|
|
|
state.loggers.set(loggerName, logger);
|
|
|
|
}
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2019-01-06 14:19:15 -05:00
|
|
|
setup(DEFAULT_CONFIG);
|