1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-27 17:49:08 -05:00
denoland-deno/log/mod.ts

133 lines
3.3 KiB
TypeScript
Raw Normal View History

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: {
default: new ConsoleHandler(DEFAULT_LEVEL)
2019-01-27 10:21:00 -05:00
},
2018-12-19 13:16:45 -05:00
loggers: {
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) {
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-03-04 19:53:35 -05:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const debug = (msg: string, ...args: any[]): void =>
getLogger("default").debug(msg, ...args);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const info = (msg: string, ...args: any[]): void =>
getLogger("default").info(msg, ...args);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const warning = (msg: string, ...args: any[]): void =>
getLogger("default").warning(msg, ...args);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const error = (msg: string, ...args: any[]): void =>
getLogger("default").error(msg, ...args);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export const critical = (msg: string, ...args: any[]): void =>
getLogger("default").critical(msg, ...args);
export function getHandler(name: string): BaseHandler {
2019-01-19 13:46:46 -05:00
return state.handlers.get(name);
}
2019-03-04 19:53:35 -05:00
export async function setup(config: LogConfig): Promise<void> {
2019-01-27 10:21:00 -05:00
state.config = {
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-04-24 07:41:23 -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-04-24 07:41:23 -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-04-24 07:41:23 -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);