|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const { AsyncLocalStorage } = require('async_hooks'); |
| 4 | + |
| 5 | +/** |
| 6 | + * Global store for request-scoped context (Correlation IDs) |
| 7 | + */ |
| 8 | +const storage = new AsyncLocalStorage(); |
| 9 | + |
| 10 | +/** |
| 11 | + * Structured Logger Engine |
| 12 | + * Issue #713: Implements high-performance JSON-based structured logging. |
| 13 | + */ |
| 14 | +class StructuredLogger { |
| 15 | + constructor() { |
| 16 | + this.logDir = path.join(process.cwd(), 'logs'); |
| 17 | + if (!fs.existsSync(this.logDir)) { |
| 18 | + fs.mkdirSync(this.logDir); |
| 19 | + } |
| 20 | + |
| 21 | + this.levels = { |
| 22 | + DEBUG: 0, |
| 23 | + INFO: 1, |
| 24 | + WARN: 2, |
| 25 | + ERROR: 3, |
| 26 | + CRITICAL: 4 |
| 27 | + }; |
| 28 | + |
| 29 | + this.minLevel = process.env.LOG_LEVEL || 'INFO'; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * Internal method to write the log entry |
| 34 | + */ |
| 35 | + _write(level, message, metadata = {}) { |
| 36 | + if (this.levels[level] < this.levels[this.minLevel]) return; |
| 37 | + |
| 38 | + const context = storage.getStore() || {}; |
| 39 | + const logEntry = { |
| 40 | + timestamp: new Date().toISOString(), |
| 41 | + level, |
| 42 | + message, |
| 43 | + traceId: context.traceId || 'system', |
| 44 | + userId: context.userId || 'anonymous', |
| 45 | + ...metadata, |
| 46 | + environment: process.env.NODE_ENV || 'development', |
| 47 | + version: process.env.APP_VERSION || '1.0.0' |
| 48 | + }; |
| 49 | + |
| 50 | + const logString = JSON.stringify(logEntry); |
| 51 | + |
| 52 | + // Output to console for cloud log collectors (like Vercel/AWS) |
| 53 | + if (level === 'ERROR' || level === 'CRITICAL') { |
| 54 | + console.error(logString); |
| 55 | + } else { |
| 56 | + console.log(logString); |
| 57 | + } |
| 58 | + |
| 59 | + // Also write to local file for development audit trails |
| 60 | + const logFile = path.join(this.logDir, `${level.toLowerCase()}.log`); |
| 61 | + fs.appendFile(logFile, logString + '\n', (err) => { |
| 62 | + if (err) console.error('Failed to write to log file:', err); |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + info(msg, meta) { this._write('INFO', msg, meta); } |
| 67 | + debug(msg, meta) { this._write('DEBUG', msg, meta); } |
| 68 | + warn(msg, meta) { this._write('WARN', msg, meta); } |
| 69 | + error(msg, meta) { this._write('ERROR', msg, meta); } |
| 70 | + critical(msg, meta) { this._write('CRITICAL', msg, meta); } |
| 71 | + |
| 72 | + /** |
| 73 | + * Provide access to the storage for middleware integration |
| 74 | + */ |
| 75 | + getStorage() { |
| 76 | + return storage; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +module.exports = new StructuredLogger(); |
0 commit comments