1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-30 16:40:57 -05:00
denoland-deno/log/mod.ts

124 lines
2.7 KiB
TypeScript
Raw Normal View History

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";
const DEFAULT_NAME = "";
2019-01-02 09:12:48 -05:00
const DEFAULT_CONFIG: LogConfig = {
2019-01-06 14:19:15 -05:00
handlers: {},
2018-12-19 13:16:45 -05:00
loggers: {
2019-01-02 09:12:48 -05:00
"": {
level: "INFO",
2019-01-06 14:19:15 -05:00
handlers: [""]
2018-12-19 13:16:45 -05:00
}
}
};
2019-01-02 09:12:48 -05:00
const defaultHandler = new ConsoleHandler("INFO");
const defaultLogger = new Logger("INFO", [defaultHandler]);
2018-12-19 13:16:45 -05:00
const state = {
2019-01-02 09:12:48 -05:00
defaultHandler,
defaultLogger,
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[]) =>
defaultLogger.debug(msg, ...args);
export const info = (msg: string, ...args: any[]) =>
defaultLogger.info(msg, ...args);
export const warning = (msg: string, ...args: any[]) =>
defaultLogger.warning(msg, ...args);
export const error = (msg: string, ...args: any[]) =>
defaultLogger.error(msg, ...args);
export const critical = (msg: string, ...args: any[]) =>
defaultLogger.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-02 09:12:48 -05:00
return defaultLogger;
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) {
state.config = config;
// 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);