-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlogger.ts
More file actions
29 lines (25 loc) · 1.01 KB
/
Copy pathlogger.ts
File metadata and controls
29 lines (25 loc) · 1.01 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
type LogLevel = 'debug' | 'info' | 'warn' | 'error';
function nowIso() {
return new Date().toISOString();
}
function write(level: LogLevel, payload: Record<string, any>) {
const entry = Object.assign({ ts: nowIso(), level }, payload);
// Emit a single-line JSON so logs are easy to parse and non-ambiguous
try {
console.log(JSON.stringify(entry));
} catch (err) {
// Fallback to plain console if serialization fails
console.log(`[${entry.ts}] ${level.toUpperCase()} ${JSON.stringify(payload)}`);
}
}
export const logger = {
debug: (msg: string, meta: Record<string, any> = {}) => {
if (process.env.DEBUG === 'true' || process.env.DEBUG === '1') {
write('debug', { msg, ...meta });
}
},
info: (msg: string, meta: Record<string, any> = {}) => write('info', { msg, ...meta }),
warn: (msg: string, meta: Record<string, any> = {}) => write('warn', { msg, ...meta }),
error: (msg: string, meta: Record<string, any> = {}) => write('error', { msg, ...meta }),
};
export default logger;