2019-02-07 11:45:47 -05:00
|
|
|
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
|
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: {
|
2019-01-28 11:17:00 -05:00
|
|
|
default: new ConsoleHandler(DEFAULT_LEVEL)
|
2019-01-27 10:21:00 -05:00
|
|
|
},
|
2018-12-19 13:16:45 -05:00
|
|
|
|
|
|
|
loggers: {
|
2019-01-28 11:17:00 -05:00
|
|
|
default: {
|
2019-01-27 10:21:00 -05:00
|
|
|
level: DEFAULT_LEVEL,
|
|
|
|
handlers: ["default"]
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const state = {
|
2019-03-04 19:53:35 -05:00
|
|
|
handlers: new Map<string, BaseHandler>(),
|
|
|
|
loggers: new Map<string, Logger>(),
|
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-03-04 19:53:35 -05:00
|
|
|
export function getLogger(name?: string): Logger {
|
2018-12-19 13:16:45 -05:00
|
|
|
if (!name) {
|
2019-05-30 08:59:30 -04: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
|
|
|
}
|
|
|
|
|
2019-05-30 08:59:30 -04:00
|
|
|
return state.loggers.get(name)!;
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
|
|
|
|
2019-06-06 12:56:33 -04:00
|
|
|
export const debug = (msg: string, ...args: unknown[]): void =>
|
2019-03-04 19:53:35 -05:00
|
|
|
getLogger("default").debug(msg, ...args);
|
2019-06-06 12:56:33 -04:00
|
|
|
export const info = (msg: string, ...args: unknown[]): void =>
|
2019-03-04 19:53:35 -05:00
|
|
|
getLogger("default").info(msg, ...args);
|
2019-06-06 12:56:33 -04:00
|
|
|
export const warning = (msg: string, ...args: unknown[]): void =>
|
2019-03-04 19:53:35 -05:00
|
|
|
getLogger("default").warning(msg, ...args);
|
2019-06-06 12:56:33 -04:00
|
|
|
export const error = (msg: string, ...args: unknown[]): void =>
|
2019-03-04 19:53:35 -05:00
|
|
|
getLogger("default").error(msg, ...args);
|
2019-06-06 12:56:33 -04:00
|
|
|
export const critical = (msg: string, ...args: unknown[]): void =>
|
2019-03-04 19:53:35 -05:00
|
|
|
getLogger("default").critical(msg, ...args);
|
|
|
|
|
|
|
|
export async function setup(config: LogConfig): Promise<void> {
|
2019-01-27 10:21:00 -05:00
|
|
|
state.config = {
|
2019-01-28 11:17:00 -05:00
|
|
|
handlers: { ...DEFAULT_CONFIG.handlers, ...config.handlers },
|
|
|
|
loggers: { ...DEFAULT_CONFIG.loggers, ...config.loggers }
|
2019-01-27 10:21:00 -05:00
|
|
|
};
|
2019-01-02 09:12:48 -05:00
|
|
|
|
|
|
|
// tear down existing handlers
|
2019-10-09 17:22:22 -04:00
|
|
|
state.handlers.forEach(
|
|
|
|
(handler): void => {
|
|
|
|
handler.destroy();
|
|
|
|
}
|
|
|
|
);
|
2019-01-02 09:12:48 -05:00
|
|
|
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 || [];
|
2019-03-04 19:53:35 -05:00
|
|
|
const handlers: BaseHandler[] = [];
|
2019-01-02 09:12:48 -05:00
|
|
|
|
2019-10-09 17:22:22 -04:00
|
|
|
handlerNames.forEach(
|
|
|
|
(handlerName): void => {
|
|
|
|
if (state.handlers.has(handlerName)) {
|
|
|
|
handlers.push(state.handlers.get(handlerName)!);
|
|
|
|
}
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
2019-10-09 17:22:22 -04:00
|
|
|
);
|
2019-01-02 09:12:48 -05:00
|
|
|
|
|
|
|
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);
|