2024-02-02 00:35:43 +00:00
|
|
|
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
|
2024-04-10 02:43:44 +00:00
|
|
|
// This module is browser compatible.
|
2024-02-02 00:35:43 +00:00
|
|
|
|
2024-02-27 21:57:25 +00:00
|
|
|
import type { BaseHandler } from "./base_handler.ts";
|
2024-02-02 00:35:43 +00:00
|
|
|
import { DEFAULT_CONFIG, DEFAULT_LEVEL } from "./_config.ts";
|
|
|
|
import { type LogConfig, Logger } from "./logger.ts";
|
|
|
|
import { state } from "./_state.ts";
|
|
|
|
|
|
|
|
/** Setup logger config. */
|
|
|
|
export function setup(config: LogConfig) {
|
|
|
|
state.config = {
|
|
|
|
handlers: { ...DEFAULT_CONFIG.handlers, ...config.handlers },
|
|
|
|
loggers: { ...DEFAULT_CONFIG.loggers, ...config.loggers },
|
|
|
|
};
|
|
|
|
|
|
|
|
// tear down existing handlers
|
|
|
|
state.handlers.forEach((handler) => {
|
|
|
|
handler.destroy();
|
|
|
|
});
|
|
|
|
state.handlers.clear();
|
|
|
|
|
|
|
|
// setup handlers
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const handlers = state.config.handlers ?? {};
|
2024-02-02 00:35:43 +00:00
|
|
|
|
|
|
|
for (const [handlerName, handler] of Object.entries(handlers)) {
|
|
|
|
handler.setup();
|
|
|
|
state.handlers.set(handlerName, handler);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove existing loggers
|
|
|
|
state.loggers.clear();
|
|
|
|
|
|
|
|
// setup loggers
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const loggers = state.config.loggers ?? {};
|
2024-02-02 00:35:43 +00:00
|
|
|
for (const [loggerName, loggerConfig] of Object.entries(loggers)) {
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const handlerNames = loggerConfig.handlers ?? [];
|
2024-02-02 00:35:43 +00:00
|
|
|
const handlers: BaseHandler[] = [];
|
|
|
|
|
|
|
|
handlerNames.forEach((handlerName) => {
|
|
|
|
const handler = state.handlers.get(handlerName);
|
|
|
|
if (handler) {
|
|
|
|
handlers.push(handler);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
refactor(archive,async,cli,csv,dotenv,encoding,expect,fmt,front-matter,fs,http,internal,log,net,path,semver,testing,text,webgpu,yaml): enable `"exactOptionalPropertyTypes"` option (#5892)
2024-09-04 05:15:01 +00:00
|
|
|
const levelName = loggerConfig.level ?? DEFAULT_LEVEL;
|
2024-02-02 00:35:43 +00:00
|
|
|
const logger = new Logger(loggerName, levelName, { handlers: handlers });
|
|
|
|
state.loggers.set(loggerName, logger);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setup(DEFAULT_CONFIG);
|