/** * Lightweight structured logger. * * Honors LOG_LEVEL (debug | info | warn | error). Defaults to 'info'. * Output is single-line JSON for easy ingestion by log shippers. * * Note: requires config-free defaults so it can be imported anywhere without * pulling in config/index.js (which validates env on load). */ const LEVELS = { debug: 10, info: 20, warn: 30, error: 40 }; function configuredLevel() { const raw = (process.env.LOG_LEVEL || 'info').toLowerCase(); return LEVELS[raw] ?? LEVELS.info; } function shouldEmit(level) { return LEVELS[level] >= configuredLevel(); } function log(level, message, meta = {}) { if (!shouldEmit(level)) return; const entry = { ts: new Date().toISOString(), level, msg: message, ...meta, }; const line = JSON.stringify(entry); if (level === 'error') console.error(line); else if (level === 'warn') console.warn(line); else console.log(line); } const logger = { debug: (msg, meta) => log('debug', msg, meta), info: (msg, meta) => log('info', msg, meta), warn: (msg, meta) => log('warn', msg, meta), error: (msg, meta) => log('error', msg, meta), }; module.exports = logger;