1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-04 17:18:23 -05:00
denoland-deno/logging/index.ts

111 lines
2.6 KiB
TypeScript
Raw Normal View History

2018-12-19 13:16:45 -05:00
import { Logger } from "./logger.ts";
2019-01-02 09:12:48 -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 = {
2018-12-19 13:16:45 -05:00
handlers: {
2019-01-02 09:12:48 -05:00
2018-12-19 13:16:45 -05:00
},
loggers: {
2019-01-02 09:12:48 -05:00
"": {
level: "INFO",
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-02 09:12:48 -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,
FileHandler,
2018-12-19 13:16:45 -05:00
};
2019-01-02 09:12:48 -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);
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-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
setup(DEFAULT_CONFIG);