import * as colors from 'jsr:@std/fmt@^1.0.4/colors'; import * as log from 'jsr:@std/log'; import { getEveFilePath } from './files.ts'; export * as log from 'jsr:@std/log'; export async function setupLogger() { const formatLevel = (level: number): string => { return ( { 10: colors.gray('[DEBUG]'), 20: colors.green('[INFO] '), 30: colors.yellow('[WARN] '), 40: colors.red('[ERROR]'), 50: colors.bgRed('[FATAL]'), }[level] || `[LVL${level}]` ); }; const levelName = (level: number): string => { return ( { 10: 'DEBUG', 20: 'INFO', 30: 'WARN', 40: 'ERROR', 50: 'FATAL', }[level] || `LVL${level}` ); }; const formatArg = (arg: unknown): string => { if (typeof arg === 'object') return JSON.stringify(arg); return String(arg); }; await log.setup({ handlers: { console: new log.ConsoleHandler('DEBUG', { useColors: true, formatter: (record) => { const timestamp = new Date().toISOString(); let msg = `${colors.dim(`[${timestamp}]`)} ${formatLevel(record.level)} ${record.msg}`; if (record.args.length > 0) { const args = record.args .map((arg, i) => `${colors.dim(`arg${i}:`)} ${formatArg(arg)}`) .join(' '); msg += ` ${colors.dim('|')} ${args}`; } return msg; }, }), file: new log.FileHandler('DEBUG', { filename: Deno.env.get('LOG_FILE') || (await getEveFilePath('eve-logs.jsonl')), formatter: (record) => { const timestamp = new Date().toISOString(); return JSON.stringify({ timestamp, level: levelName(record.level), msg: record.msg, args: record.args, }); }, }), }, loggers: { default: { level: 'DEBUG', handlers: ['console', 'file'], }, }, }); }