this repo has no description
at export-config 50 lines 1.3 kB view raw
1import * as fs from "node:fs"; 2import * as path from "node:path"; 3 4const DATA_DIR = process.env.DATA_DIR || "./data"; 5const LOG_PATH = path.join(DATA_DIR, "app.log"); 6 7type LogLevel = "INFO" | "WARN" | "ERROR" | "DEBUG"; 8 9function formatDate(date: Date): string { 10 return date.toISOString(); 11} 12 13function writeLog( 14 level: LogLevel, 15 message: string, 16 meta?: Record<string, any>, 17) { 18 const timestamp = formatDate(new Date()); 19 const metaStr = meta ? ` ${JSON.stringify(meta)}` : ""; 20 const logLine = `[${timestamp}] ${level}: ${message}${metaStr}\n`; 21 22 // Write to file 23 try { 24 fs.appendFileSync(LOG_PATH, logLine); 25 } catch (err) { 26 // Fall back to console if file write fails 27 console.error("Failed to write to log file:", err); 28 } 29 30 // Also write to stdout/stderr for systemd journal 31 if (level === "ERROR") { 32 process.stderr.write(logLine); 33 } else { 34 process.stdout.write(logLine); 35 } 36} 37 38export const logger = { 39 info: (message: string, meta?: Record<string, any>) => 40 writeLog("INFO", message, meta), 41 warn: (message: string, meta?: Record<string, any>) => 42 writeLog("WARN", message, meta), 43 error: (message: string, meta?: Record<string, any>) => 44 writeLog("ERROR", message, meta), 45 debug: (message: string, meta?: Record<string, any>) => { 46 if (process.env.DEBUG) { 47 writeLog("DEBUG", message, meta); 48 } 49 }, 50};