-
Notifications
You must be signed in to change notification settings - Fork 7
/
logger.ts
90 lines (83 loc) · 2.28 KB
/
logger.ts
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
import { colors, log } from "../../../deps.ts";
import { introspectors } from "../../../plugins/mod.ts";
/**
* Behaves like the ConsoleHandler, but doesn't change the
* output color. This is left to the formatter function.
*/
class RawConsoleLogHandler extends log.handlers.BaseHandler {
log(msg: string): void {
console.log(msg);
}
}
/**
* The verbose log handler outputs timestamps, log level, the logger name
* and doesn't change the color of the output
*/
function verboseHandler() {
return new RawConsoleLogHandler("DEBUG", {
formatter: (logRecord) => {
const { datetime, levelName, loggerName, msg } = logRecord;
const time = datetime.toISOString();
return `[${time}] ${levelName}:${loggerName} - ${msg}`;
},
});
}
/**
* The default log handler outputs messages with colors for each respective
* log level
*/
function defaultHandler() {
const info = colors.bold.blue;
const warning = colors.bold.yellow;
const error = colors.bold;
const critical = colors.bold.red;
return new RawConsoleLogHandler("INFO", {
formatter: (logRecord) => {
const { levelName, msg } = logRecord;
switch (levelName) {
case "INFO":
return info(msg);
case "WARNING":
return warning(msg);
case "ERROR":
return error(msg);
case "CRITICAL":
return critical(msg);
default:
return msg;
}
},
});
}
/**
* Discover plugins to generate one logger per plugin
*/
function pluginLoggerNames() {
return introspectors.map((i) => i.name);
}
/**
* Configure the logger for the CLI app
* @param debug if debug is `true` the logger level decrease from INFO to DEBUG
*/
export async function setupLogger(debug?: boolean) {
const logLevel = debug ? "DEBUG" : "INFO";
const logHandler = debug ? verboseHandler() : defaultHandler();
const loggerConfig: log.LoggerConfig = {
level: logLevel,
handlers: ["console"],
};
// Create the static main and default loggers, add one logger per plugin
const loggers: log.LogConfig["loggers"] = {
default: loggerConfig,
main: loggerConfig,
};
for (const plugin of pluginLoggerNames()) {
loggers[plugin] = loggerConfig;
}
await log.setup({
handlers: {
console: logHandler,
},
loggers,
});
}