blob: 5fabff60f34d97be5c0d1f722eb711f57be4c17c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
import { Logger } from "./logger.ts";
import { BaseHandler } from "./handler.ts";
import { ConsoleHandler } from "./handlers/console.ts";
export interface HandlerConfig {
// TODO: replace with type describing class derived from BaseHandler
class: typeof BaseHandler;
level?: string;
}
export class LoggerConfig {
level?: string;
handlers?: string[];
}
export interface LoggingConfig {
handlers?: {
[name: string]: HandlerConfig;
};
loggers?: {
[name: string]: LoggerConfig;
};
}
const DEFAULT_LEVEL = "INFO";
const DEFAULT_NAME = "";
const DEFAULT_CONFIG: LoggingConfig = {
handlers: {
[DEFAULT_NAME]: {
level: DEFAULT_LEVEL,
class: ConsoleHandler
}
},
loggers: {
[DEFAULT_NAME]: {
level: DEFAULT_LEVEL,
handlers: [DEFAULT_NAME]
}
}
};
const state = {
loggers: new Map(),
config: DEFAULT_CONFIG
};
function createNewHandler(name: string) {
let handlerConfig = state.config.handlers[name];
if (!handlerConfig) {
handlerConfig = state.config.handlers[DEFAULT_NAME];
}
const constructor = handlerConfig.class;
console.log(constructor);
const handler = new constructor(handlerConfig.level);
return handler;
}
function createNewLogger(name: string) {
let loggerConfig = state.config.loggers[name];
if (!loggerConfig) {
loggerConfig = state.config.loggers[DEFAULT_NAME];
}
const handlers = (loggerConfig.handlers || []).map(createNewHandler);
const levelName = loggerConfig.level || DEFAULT_LEVEL;
return new Logger(levelName, handlers);
}
export const handlers = {
BaseHandler: BaseHandler,
ConsoleHandler: ConsoleHandler
};
export function getLogger(name?: string) {
if (!name) {
name = DEFAULT_NAME;
}
if (!state.loggers.has(name)) {
return createNewLogger(name);
}
return state.loggers.get(name);
}
export function setup(config: LoggingConfig) {
state.config = {
handlers: {
...DEFAULT_CONFIG.handlers,
...config.handlers!
},
loggers: {
...DEFAULT_CONFIG.loggers,
...config.loggers!
}
};
}
|