const fs = require('fs'); const path = require('path'); const LOG_FILE = path.join(__dirname, '..', 'compress.log'); const LEVEL_PRIORITY = { error: 0, warn: 1, info: 2, debug: 3 }; const currentLevel = LEVEL_PRIORITY[process.env.LOG_LEVEL || 'info'] ?? 2; function formatTimestamp() { return new Date().toISOString(); } function log(level, msg, meta) { if ((LEVEL_PRIORITY[level] ?? 2) > currentLevel) return; const ts = formatTimestamp(); const metaStr = meta ? ' ' + JSON.stringify(meta) : ''; const line = `[${ts}] [${level.toUpperCase()}] ${msg}${metaStr}`; process.stdout.write(line + '\n'); } function logToFile(text) { const ts = formatTimestamp(); fs.appendFile(LOG_FILE, `[${ts}] ${text}\n`, () => {}); } module.exports = { info: (msg, meta) => log('info', msg, meta), warn: (msg, meta) => log('warn', msg, meta), error: (msg, meta) => log('error', msg, meta), debug: (msg, meta) => log('debug', msg, meta), logToFile, };