-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.ts
More file actions
199 lines (166 loc) · 5.28 KB
/
Copy pathlogger.ts
File metadata and controls
199 lines (166 loc) · 5.28 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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Simple ANSI color helpers for zero-dependency logging
const color = (code: number) => (str: string) => `\x1b[${code}m${str}\x1b[0m`;
const pc = {
gray: color(90),
blue: color(34),
yellow: color(33),
red: color(31),
cyan: color(36),
magenta: color(35),
white: color(37),
dim: (str: string) => `\x1b[2m${str}\x1b[0m`,
bold: (str: string) => `\x1b[1m${str}\x1b[0m`,
};
export enum LogLevel {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3,
}
export interface LogEntry {
level: keyof typeof LogLevel;
namespace: string;
message: string;
timestamp: string;
data?: any;
durationMs?: number;
}
export type Transport = (entry: LogEntry) => void;
export interface Logger {
(message: string, ...args: any[]): void;
debug(message: string, data?: any): void;
info(message: string, data?: any): void;
warn(message: string, data?: any): void;
error(message: string, data?: any): void;
measure<T>(name: string, fn: () => Promise<T> | T): Promise<T>;
enabled: boolean;
}
// ─── Configuration ────────────────────────────────────────────────────────────
const env = typeof process !== 'undefined' ? process.env : {};
let currentLogLevel = LogLevel.INFO;
const rawLevel = (env.ILN_LOG_LEVEL || '').toUpperCase();
if (rawLevel in LogLevel) {
currentLogLevel = LogLevel[rawLevel as keyof typeof LogLevel];
} else if (env.ILN_DEBUG === '1') {
currentLogLevel = LogLevel.DEBUG;
}
const isJsonFormat = env.ILN_LOG_FORMAT === 'json';
const transports: Transport[] = [];
// Default console transport
const defaultTransport: Transport = (entry) => {
if (isJsonFormat) {
console.log(JSON.stringify(entry));
return;
}
const colorMap = {
DEBUG: pc.gray,
INFO: pc.blue,
WARN: pc.yellow,
ERROR: pc.red,
};
const levelColor = colorMap[entry.level] || pc.white;
const timeStr = pc.dim(new Date(entry.timestamp).toLocaleTimeString());
const nsStr = pc.cyan(entry.namespace);
const durStr = entry.durationMs ? pc.magenta(` (+${entry.durationMs}ms)`) : '';
let msg = `${timeStr} ${levelColor(entry.level.padEnd(5))} [${nsStr}] ${entry.message}${durStr}`;
if (entry.data) {
msg += ` ${pc.dim(JSON.stringify(entry.data))}`;
}
if (entry.level === 'ERROR') {
console.error(msg);
} else {
console.log(msg);
}
};
transports.push(defaultTransport);
// ─── Implementation ───────────────────────────────────────────────────────────
class LoggerImpl {
public enabled: boolean;
constructor(private namespace: string) {
this.enabled = LogLevel.DEBUG >= currentLogLevel;
}
// Implementation of the function signature
// We use a trick to make the class instance callable
public static create(namespace: string): Logger {
const instance = new LoggerImpl(namespace);
const logger = ((msg: string, ...args: any[]) => {
instance.debug(msg, args.length > 1 ? args : args[0]);
}) as unknown as Logger;
logger.debug = instance.debug.bind(instance);
logger.info = instance.info.bind(instance);
logger.warn = instance.warn.bind(instance);
logger.error = instance.error.bind(instance);
logger.measure = instance.measure.bind(instance);
logger.enabled = instance.enabled;
(logger as any).namespace = namespace;
return logger;
}
public debug(message: string, data?: any): void {
this.log('DEBUG', message, data);
}
public info(message: string, data?: any): void {
this.log('INFO', message, data);
}
public warn(message: string, data?: any): void {
this.log('WARN', message, data);
}
public error(message: string, data?: any): void {
this.log('ERROR', message, data);
}
public async measure<T>(name: string, fn: () => Promise<T> | T): Promise<T> {
const start = Date.now();
try {
const result = await fn();
const durationMs = Date.now() - start;
this.log('DEBUG', `Measured ${name}`, { durationMs });
return result;
} catch (err) {
const durationMs = Date.now() - start;
this.log('ERROR', `Failed ${name} after ${durationMs}ms`, { error: err });
throw err;
}
}
private log(
levelStr: keyof typeof LogLevel,
message: string,
data?: any,
durationMs?: number
): void {
const level = LogLevel[levelStr];
if (level < currentLogLevel) return;
const entry: LogEntry = {
level: levelStr,
namespace: this.namespace,
message,
timestamp: new Date().toISOString(),
data,
durationMs,
};
for (const transport of transports) {
try {
transport(entry);
} catch (err) {
// Fallback to basic console if transport fails
console.error('Logger transport failed', err);
}
}
}
}
/**
* Creates a new logger instance for the given namespace.
*/
export function createLogger(namespace: string): Logger {
return LoggerImpl.create(namespace);
}
/**
* Adds a custom transport to the logger.
*/
export function addTransport(transport: Transport): void {
transports.push(transport);
}
/**
* Manually sets the log level.
*/
export function setLogLevel(level: LogLevel): void {
currentLogLevel = level;
}