Spaces:
Sleeping
Sleeping
| const fs = require("fs"); | |
| const path = require("path"); | |
| const logDir = path.join(__dirname, "logs"); | |
| if (!fs.existsSync(logDir)) { | |
| fs.mkdirSync(logDir, { recursive: true }); | |
| } | |
| const logFile = path.join(logDir, "combined.log"); | |
| const logStream = fs.createWriteStream(logFile, { flags: "a", encoding: "utf8" }); | |
| function formatMessage(level, args) { | |
| const timestamp = new Date().toISOString(); | |
| const message = args.map((arg) => { | |
| if (arg instanceof Error) { | |
| return arg.stack || arg.message || String(arg); | |
| } | |
| if (typeof arg === "object" && arg !== null) { | |
| try { | |
| return JSON.stringify(arg, null, 2); | |
| } catch (e) { | |
| return String(arg); | |
| } | |
| } | |
| return String(arg); | |
| }).join(" "); | |
| return `[${timestamp}] [${level}] ${message}\n`; | |
| } | |
| // Keep original references | |
| const originalLog = console.log; | |
| const originalError = console.error; | |
| const originalWarn = console.warn; | |
| const originalInfo = console.info; | |
| console.log = function (...args) { | |
| originalLog.apply(console, args); | |
| logStream.write(formatMessage("INFO", args)); | |
| }; | |
| console.error = function (...args) { | |
| originalError.apply(console, args); | |
| logStream.write(formatMessage("ERROR", args)); | |
| }; | |
| console.warn = function (...args) { | |
| originalWarn.apply(console, args); | |
| logStream.write(formatMessage("WARN", args)); | |
| }; | |
| console.info = function (...args) { | |
| originalInfo.apply(console, args); | |
| logStream.write(formatMessage("INFO", args)); | |
| }; | |
| console.debug = function (...args) { | |
| originalLog.apply(console, ["DEBUG:", ...args]); | |
| logStream.write(formatMessage("DEBUG", args)); | |
| }; | |
| module.exports = { | |
| logStream, | |
| }; | |