-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.ts
112 lines (99 loc) · 2.76 KB
/
log.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import * as Colors from "std/fmt/colors.ts";
import {
critical,
debug,
error,
info,
setup as _setup,
warning,
} from "std/log/mod.ts";
import { ConsoleHandler } from "std/log/handlers.ts";
import { LogRecord } from "std/log/logger.ts";
import { LevelName, LogLevelNames } from "std/log/levels.ts";
import { RoomId } from "./sfuServers.ts";
export { LogLevelNames };
export type { LevelName };
const start = Date.now();
const symbols: { [Level in LevelName]: string } = {
NOTSET: " ",
DEBUG: "⚫",
INFO: "🔵",
WARNING: "🟡",
ERROR: "🔴",
CRITICAL: "💥",
};
export type LogArgs = Record<
string,
(string | number | boolean | RoomId | null)
>;
export { critical, debug, error, info, warning };
const messageColors: { [Level in LevelName]: ((_: string) => string) } = {
NOTSET: Colors.reset,
DEBUG: Colors.dim,
INFO: Colors.reset,
WARNING: Colors.reset,
ERROR: Colors.bold,
CRITICAL: (text) => Colors.bold(Colors.inverse(Colors.red(text))),
};
export type LogFormat = "pretty" | "json";
export function setup(level: LevelName, type: LogFormat) {
const Handler = type == "pretty" ? PrettyHandler : JsonHandler;
_setup({
handlers: {
default: new Handler(level),
},
loggers: {
default: {
level,
handlers: ["default"],
},
},
});
}
class PrettyHandler extends ConsoleHandler {
format(logRecord: LogRecord): string {
const level = logRecord.levelName as LevelName;
const time = relativeTime(logRecord.datetime);
return `${Colors.reset(time)} ${symbols[level]} ${
messageColors[level](logRecord.msg)
}${formatArgs({ args: logRecord.args[0] as LogArgs, colors: true })}`;
}
}
class JsonHandler extends ConsoleHandler {
format(logRecord: LogRecord): string {
const severity = logRecord.levelName;
const ts = logRecord.datetime.toISOString();
const msg = logRecord.msg;
const args = logRecord.args[0] as LogArgs;
return JSON.stringify({
ts,
severity,
msg,
...args,
});
}
}
function formatArgs(
{ args, colors }: { args: LogArgs | undefined; colors: boolean },
): string {
if (args === undefined) {
return "";
} else {
return Deno.inspect(args, {
compact: false,
colors,
trailingComma: true,
}).slice(1, -2);
}
}
function divmod(numerator: number, denominator: number): [number, number] {
return [Math.floor(numerator / denominator), numerator % denominator];
}
function relativeTime(date: Date): string {
let milliseconds, offset = date.valueOf() - start;
[offset, milliseconds] = divmod(offset, 1000);
const [minutes, seconds] = divmod(offset, 60);
return `${minutes.toString().padStart(4, "0")}:${
seconds.toString().padStart(2, "0")
}.${milliseconds.toString().padStart(4, "0")}`;
}