|
| 1 | +import fs from "fs"; |
| 2 | +import path from "path"; |
| 3 | + |
| 4 | +type LogLevel = "info" | "warn" | "error" | "debug"; |
| 5 | + |
| 6 | +type Mode = "memory" | "stream"; |
| 7 | + |
| 8 | +interface LoggerConfig { |
| 9 | + mode?: Mode; // default: "memory" |
| 10 | + logDir?: string; // default: "logs" |
| 11 | + singleFile?: boolean; // default: false (per input file logs) |
| 12 | + levels?: { |
| 13 | + stdout?: LogLevel[]; // default: ["warn", "error"] |
| 14 | + file?: LogLevel[]; // default: ["info", "warn", "error"] |
| 15 | + }; |
| 16 | +} |
| 17 | + |
| 18 | +interface LogEntry { |
| 19 | + timestamp: string; |
| 20 | + level: LogLevel; |
| 21 | + message: string; |
| 22 | +} |
| 23 | + |
| 24 | +export const createLogger = (config: LoggerConfig = {}) => { |
| 25 | + const mode: Mode = config.mode ?? "memory"; |
| 26 | + const logDir = config.logDir ?? "logs"; |
| 27 | + const singleFile = config.singleFile ?? false; |
| 28 | + const levels = { |
| 29 | + stdout: config.levels?.stdout ?? ["warn", "error"], |
| 30 | + file: config.levels?.file ?? ["info", "warn", "error"], |
| 31 | + }; |
| 32 | + |
| 33 | + if (!fs.existsSync(logDir)) fs.mkdirSync(logDir, { recursive: true }); |
| 34 | + |
| 35 | + const buffers = new Map<string, LogEntry[]>(); |
| 36 | + const streams = new Map<string, fs.WriteStream>(); |
| 37 | + |
| 38 | + const getStream = (fileId: string) => { |
| 39 | + const key = singleFile ? "all" : fileId; |
| 40 | + if (!streams.has(key)) { |
| 41 | + const filePath = path.join(logDir, singleFile ? "combined.log" : `${fileId}.log`); |
| 42 | + streams.set(key, fs.createWriteStream(filePath, { flags: "a" })); |
| 43 | + } |
| 44 | + return streams.get(key)!; |
| 45 | + }; |
| 46 | + |
| 47 | + const write = (fileId: string, level: LogLevel, message: string) => { |
| 48 | + const entry: LogEntry = { timestamp: new Date().toISOString(), level, message }; |
| 49 | + |
| 50 | + // Console output if enabled |
| 51 | + if (levels.stdout.includes(level)) { |
| 52 | + const fn = level === "error" ? console.error : console.log; |
| 53 | + fn(`[${fileId}] [${entry.timestamp}] [${level.toUpperCase()}] ${entry.message}`); |
| 54 | + } |
| 55 | + |
| 56 | + // File output |
| 57 | + if (levels.file.includes(level)) { |
| 58 | + if (mode === "memory") { |
| 59 | + if (!buffers.has(fileId)) buffers.set(fileId, []); |
| 60 | + buffers.get(fileId)!.push(entry); |
| 61 | + } else { |
| 62 | + getStream(fileId).write(`[${entry.timestamp}] [${level.toUpperCase()}] ${entry.message}\n`); |
| 63 | + } |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + const flush = () => { |
| 68 | + if (mode === "memory") { |
| 69 | + for (const [fileId, entries] of buffers.entries()) { |
| 70 | + const filePath = path.join(logDir, singleFile ? "combined.log" : `${fileId}.log`); |
| 71 | + const lines = entries.map(e => `[${e.timestamp}] [${e.level.toUpperCase()}] ${e.message}`); |
| 72 | + fs.writeFileSync(filePath, lines.join("\n") + "\n", { flag: "a" }); |
| 73 | + } |
| 74 | + } |
| 75 | + for (const s of streams.values()) s.end(); |
| 76 | + }; |
| 77 | + |
| 78 | + return { |
| 79 | + info: (fileId: string, msg: string) => write(fileId, "info", msg), |
| 80 | + warn: (fileId: string, msg: string) => write(fileId, "warn", msg), |
| 81 | + error: (fileId: string, msg: string) => write(fileId, "error", msg), |
| 82 | + debug: (fileId: string, msg: string) => write(fileId, "debug", msg), |
| 83 | + flush, |
| 84 | + }; |
| 85 | +}; |
| 86 | + |
| 87 | +export const globalLogger = createLogger(); |
0 commit comments