-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger.ts
More file actions
58 lines (50 loc) · 1.44 KB
/
logger.ts
File metadata and controls
58 lines (50 loc) · 1.44 KB
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
import { env } from "./env.ts";
import type { LogRecord } from "https://deno.land/std@0.179.0/log/logger.ts";
import type { HandlerOptions } from "https://deno.land/std@0.179.0/log/handlers.ts";
import { getLevelByName, log, Sentry } from "./deps.ts";
class PaperspaceLogger extends log.handlers.ConsoleHandler {
constructor(options: HandlerOptions) {
super("NOTSET", options);
}
override handle(logRecord: LogRecord) {
if (
!env.get("LOG_LEVEL") ||
// @ts-expect-error: it's fine, we are professionals
getLevelByName(env.get("LOG_LEVEL").toUpperCase()) > logRecord.level
) {
return;
}
const msg = this.format(logRecord);
return this.log(msg);
}
}
log.setup({
handlers: {
console: new PaperspaceLogger({
formatter(logState) {
Sentry.addBreadcrumb({
level: sentrySeverity[logState.levelName] ?? "info",
message: logState.msg,
});
return `${logState.levelName.padEnd(8, " ")} | ${logState.msg}`;
},
}),
},
loggers: {
default: {
handlers: ["console"],
},
},
});
const sentrySeverity: Record<string, Sentry.SeverityLevel> = {
debug: "debug",
info: "info",
warning: "warning",
error: "error",
critical: "fatal",
};
/**
* We use a `default` logger so that we can always have the most up-to-date
* log level. This is important because the log level can by flags.
*/
export const logger = log.getLogger("default");