forked from planetarium/Corvette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logUtils.ts
67 lines (60 loc) · 1.73 KB
/
logUtils.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
import { format as formatDate } from "std/datetime/mod.ts";
import {
Logger,
type LoggerConfig,
LogLevels,
type LogRecord,
} from "std/log/mod.ts";
import { type LevelName, LogLevelNames } from "std/log/levels.ts";
import { combinedEnv, LogLevelEnvKey } from "./envUtils.ts";
// outside facing component logger names
export const ObserverLoggerName = "observer";
export const EmitterLoggerName = "emitter";
export const ApiLoggerName = "api";
export const WebLoggerName = "web";
// dev utility logger names
export const DevLoggerName = "dev";
export const TestWebhookReceiverLoggerName = "testWebhookReceiver";
// internal logger names
export const ControlLoggerName = "control";
export function defaultLogFormatter(rec: LogRecord) {
let level: string;
switch (rec.level) {
case LogLevels.INFO:
level = "INFO";
break;
case LogLevels.WARNING:
level = "WARN";
break;
case LogLevels.ERROR:
level = "EROR";
break;
case LogLevels.CRITICAL:
level = "CRIT";
break;
default:
level = "DBUG";
break;
}
return `[${
formatDate(new Date(), "yyyy-MM-dd HH:mm:ss")
} ${level}][${rec.loggerName}] ${
[rec.msg, ...rec.args.map(Logger.prototype.asString)].join(" ")
}`;
}
export function getLoggingLevel() {
const level = combinedEnv[LogLevelEnvKey] || "NOTSET";
if (!LogLevelNames.includes(level)) {
throw new Error(
`${LogLevelEnvKey} must be set to one of '${
LogLevelNames.join("', '")
}'.`,
);
}
return level as LevelName;
}
export function getInternalLoggers(config: LoggerConfig) {
return [ControlLoggerName].reduce((acc, loggerName) => {
return { ...acc, [loggerName]: config };
}, {} as Record<string, LoggerConfig>);
}