chatapi / src /utils /logger.ts
wilooper's picture
initial commit
87cb242
Raw History Blame Contribute Delete
967 Bytes
import { CONFIG } from '../config';
type Level = 'info' | 'warn' | 'error' | 'debug';
const LEVELS: Record<Level, number> = { debug: 0, info: 1, warn: 2, error: 3 };
const MIN_LEVEL = CONFIG.NODE_ENV === 'production' ? LEVELS.info : LEVELS.debug;
function log(level: Level, msg: string, meta?: Record<string, unknown>): void {
if (LEVELS[level] < MIN_LEVEL) return;
const entry = JSON.stringify({
ts: new Date().toISOString(),
level,
msg,
...(meta ?? {}),
});
if (level === 'error') {
process.stderr.write(entry + '\n');
} else {
process.stdout.write(entry + '\n');
}
}
export const logger = {
info: (msg: string, meta?: Record<string, unknown>) => log('info', msg, meta),
warn: (msg: string, meta?: Record<string, unknown>) => log('warn', msg, meta),
error: (msg: string, meta?: Record<string, unknown>) => log('error', msg, meta),
debug: (msg: string, meta?: Record<string, unknown>) => log('debug', msg, meta),
};