-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathlog.ts
More file actions
131 lines (109 loc) · 2.95 KB
/
Copy pathlog.ts
File metadata and controls
131 lines (109 loc) · 2.95 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { getEnvUniversal } from "@/utils";
import {
type LevelIndex,
LevelNameMap,
type LogLevel,
LogLevels,
} from "./log-levels";
import { type LogEntry, castToLogValue, stringify } from "./logfmt";
interface LogRecord {
args: unknown[];
datetime: Date;
level: number;
levelName: string;
loggerName: string;
msg: string;
}
export class Logger {
name: string;
level: LogLevel;
constructor(name: string, level: LogLevel) {
this.name = name;
this.level = level;
}
log(level: LevelIndex, message: string, ...args: unknown[]): void {
const record: LogRecord = {
msg: message,
args,
level,
loggerName: this.name,
datetime: new Date(),
levelName: LevelNameMap[level],
};
if (this.#shouldLog(level)) {
this.#logRecord(record);
}
}
#shouldLog(level: LevelIndex): boolean {
return level >= LogLevels[this.level];
}
#logRecord(record: LogRecord): void {
console.log(formatter(record));
}
trace(message: string, ...args: unknown[]): void {
this.log(LogLevels.TRACE, message, ...args);
}
debug(message: string, ...args: unknown[]): void {
this.log(LogLevels.DEBUG, message, ...args);
}
info(message: string, ...args: unknown[]): void {
this.log(LogLevels.INFO, message, ...args);
}
warn(message: string, ...args: unknown[]): void {
this.log(LogLevels.WARN, message, ...args);
}
error(message: string, ...args: unknown[]): void {
this.log(LogLevels.ERROR, message, ...args);
}
critical(message: string, ...args: unknown[]): void {
this.log(LogLevels.CRITICAL, message, ...args);
}
}
const loggers: Record<string, Logger> = {};
export function getLogger(name = "default"): Logger {
const defaultLogLevelEnv: LogLevel | undefined = getEnvUniversal(
"_LOG_LEVEL",
) as LogLevel | undefined;
const defaultLogLevel: LogLevel = defaultLogLevelEnv ?? "INFO";
if (!loggers[name]) {
loggers[name] = new Logger(name, defaultLogLevel);
}
return loggers[name];
}
function formatter(log: LogRecord): string {
const args: LogEntry[] = [];
for (let i = 0; i < log.args.length; i++) {
const logArg = log.args[i];
if (logArg && typeof logArg === "object") {
// Spread object
for (const k in logArg) {
// biome-ignore lint/suspicious/noExplicitAny: Unknown type
const v = (logArg as any)[k];
pushArg(k, v, args);
}
} else {
pushArg(`arg${i}`, logArg, args);
}
}
return stringify(
//["ts", formatTimestamp(log.datetime)],
["level", LevelNameMap[log.level]],
// ["target", log.loggerName],
["msg", log.msg],
...args,
);
}
function pushArg(k: string, v: unknown, args: LogEntry[]) {
args.push([k, castToLogValue(v)]);
}
// function getEnv(name: string): string | undefined {
// if (typeof window !== "undefined" && window.localStorage) {
// return window.localStorage.getItem(name) || undefined;
// }
// return undefined;
// // TODO(ACTR-9): Add back env config once node compat layer works
// //return crossGetEnv(name);
// }
export function setupLogging() {
// Do nothing for now
}