2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 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,
|
2020-03-28 13:03:49 -04:00
|
|
|
FileHandler,
|
2020-04-09 07:45:24 -04:00
|
|
|
RotatingFileHandler,
|
2019-01-06 14:19:15 -05:00
|
|
|
} from "./handlers.ts";
|
2020-06-07 09:20:33 -04:00
|
|
|
import { assert } from "../_util/assert.ts";
|
2020-04-25 05:13:26 -04:00
|
|
|
import { LevelName } from "./levels.ts";
|
|
|
|
|
|
|
|
export { LogLevels } from "./levels.ts";
|
2018-12-19 13:16:45 -05:00
|
|
|
|
|
|
|
export class LoggerConfig {
|
2020-04-25 05:13:26 -04:00
|
|
|
level?: LevelName;
|
2018-12-19 13:16:45 -05:00
|
|
|
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: {
|
2020-03-28 13:03:49 -04: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,
|
2020-03-28 13:03:49 -04:00
|
|
|
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>(),
|
2020-03-28 13:03:49 -04: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,
|
2020-03-28 13:03:49 -04:00
|
|
|
FileHandler,
|
2020-04-09 07:45:24 -04:00
|
|
|
RotatingFileHandler,
|
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) {
|
2020-02-07 02:23:38 -05:00
|
|
|
const d = state.loggers.get("default");
|
|
|
|
assert(
|
|
|
|
d != null,
|
|
|
|
`"default" logger must be set for getting logger without name`
|
|
|
|
);
|
|
|
|
return d;
|
2018-12-19 13:16:45 -05:00
|
|
|
}
|
2020-02-07 02:23:38 -05:00
|
|
|
const result = state.loggers.get(name);
|
|
|
|
if (!result) {
|
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
|
|
|
}
|
2020-02-07 02:23:38 -05:00
|
|
|
return result;
|
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 },
|
2020-03-28 13:03:49 -04:00
|
|
|
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-11-13 13:42:34 -05: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-11-13 13:42:34 -05:00
|
|
|
handlerNames.forEach((handlerName): void => {
|
2020-02-07 02:23:38 -05:00
|
|
|
const handler = state.handlers.get(handlerName);
|
|
|
|
if (handler) {
|
|
|
|
handlers.push(handler);
|
2019-01-02 09:12:48 -05:00
|
|
|
}
|
2019-11-13 13:42:34 -05: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
|
|
|
|
2020-05-20 10:27:01 -04:00
|
|
|
await setup(DEFAULT_CONFIG);
|