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";
|
2020-08-24 19:43:54 -04:00
|
|
|
import type { GenericFunction } from "./logger.ts";
|
2019-01-06 14:19:15 -05:00
|
|
|
import {
|
|
|
|
BaseHandler,
|
|
|
|
ConsoleHandler,
|
2020-03-28 13:03:49 -04:00
|
|
|
FileHandler,
|
2020-04-09 07:45:24 -04:00
|
|
|
RotatingFileHandler,
|
2020-09-27 06:22:32 -04:00
|
|
|
WriterHandler,
|
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-07-08 05:26:39 -04:00
|
|
|
import type { LevelName } from "./levels.ts";
|
2020-04-25 05:13:26 -04:00
|
|
|
|
2020-08-12 06:01:36 -04:00
|
|
|
export { LogLevels } from "./levels.ts";
|
|
|
|
export type { LevelName } from "./levels.ts";
|
2020-07-05 23:57:31 -04:00
|
|
|
export { Logger } from "./logger.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
|
|
|
};
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Get a logger instance. If not specified `name`, get the default logger. */
|
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,
|
2020-07-14 15:24:17 -04:00
|
|
|
`"default" logger must be set for getting logger without name`,
|
2020-02-07 02:23:38 -05:00
|
|
|
);
|
|
|
|
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) {
|
2020-06-18 06:50:18 -04:00
|
|
|
const logger = new Logger(name, "NOTSET", { handlers: [] });
|
2019-01-02 09:12:48 -05:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Log with debug level, using default logger. */
|
2020-06-12 09:27:41 -04:00
|
|
|
export function debug<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
|
|
|
export function debug<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: T extends GenericFunction ? never : T,
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T;
|
|
|
|
export function debug<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
|
|
|
// Assist TS compiler with pass-through generic type
|
|
|
|
if (msg instanceof Function) {
|
|
|
|
return getLogger("default").debug(msg, ...args);
|
|
|
|
}
|
|
|
|
return getLogger("default").debug(msg, ...args);
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Log with info level, using default logger. */
|
2020-06-12 09:27:41 -04:00
|
|
|
export function info<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
|
|
|
export function info<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: T extends GenericFunction ? never : T,
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T;
|
|
|
|
export function info<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
|
|
|
// Assist TS compiler with pass-through generic type
|
|
|
|
if (msg instanceof Function) {
|
|
|
|
return getLogger("default").info(msg, ...args);
|
|
|
|
}
|
|
|
|
return getLogger("default").info(msg, ...args);
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Log with warning level, using default logger. */
|
2020-06-12 09:27:41 -04:00
|
|
|
export function warning<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
|
|
|
export function warning<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: T extends GenericFunction ? never : T,
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T;
|
|
|
|
export function warning<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
|
|
|
// Assist TS compiler with pass-through generic type
|
|
|
|
if (msg instanceof Function) {
|
|
|
|
return getLogger("default").warning(msg, ...args);
|
|
|
|
}
|
|
|
|
return getLogger("default").warning(msg, ...args);
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Log with error level, using default logger. */
|
2020-06-12 09:27:41 -04:00
|
|
|
export function error<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
|
|
|
export function error<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: T extends GenericFunction ? never : T,
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T;
|
|
|
|
export function error<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
|
|
|
// Assist TS compiler with pass-through generic type
|
|
|
|
if (msg instanceof Function) {
|
|
|
|
return getLogger("default").error(msg, ...args);
|
|
|
|
}
|
|
|
|
return getLogger("default").error(msg, ...args);
|
|
|
|
}
|
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Log with critical level, using default logger. */
|
2020-06-12 09:27:41 -04:00
|
|
|
export function critical<T>(msg: () => T, ...args: unknown[]): T | undefined;
|
|
|
|
export function critical<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: T extends GenericFunction ? never : T,
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T;
|
|
|
|
export function critical<T>(
|
2020-08-24 19:43:54 -04:00
|
|
|
msg: (T extends GenericFunction ? never : T) | (() => T),
|
2020-06-12 09:27:41 -04:00
|
|
|
...args: unknown[]
|
|
|
|
): T | undefined {
|
|
|
|
// Assist TS compiler with pass-through generic type
|
|
|
|
if (msg instanceof Function) {
|
|
|
|
return getLogger("default").critical(msg, ...args);
|
|
|
|
}
|
|
|
|
return getLogger("default").critical(msg, ...args);
|
|
|
|
}
|
2019-03-04 19:53:35 -05:00
|
|
|
|
2020-11-05 21:33:59 -05:00
|
|
|
/** Setup logger config. */
|
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 = {
|
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;
|
2020-06-18 06:50:18 -04:00
|
|
|
const logger = new Logger(loggerName, levelName, { handlers: handlers });
|
2019-01-02 09:12:48 -05:00
|
|
|
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);
|