|
| 1 | +/** |
| 2 | + * Per-Module Logging Utility (P7.4) |
| 3 | + * |
| 4 | + * Provides structured logging for individual module processing. |
| 5 | + * Logs are written to files organized by run timestamp and module ID. |
| 6 | + */ |
| 7 | + |
| 8 | +import { ensureDirectory } from "../../scripts/shared/fs-utils.js"; |
| 9 | +import fs from "node:fs/promises"; |
| 10 | +import path from "node:path"; |
| 11 | + |
| 12 | +/** |
| 13 | + * @typedef {Object} ModuleLoggerOptions |
| 14 | + * @property {string} projectRoot - Project root directory |
| 15 | + * @property {string} runId - Unique run identifier (timestamp) |
| 16 | + * @property {string} moduleId - Module identifier (name-----maintainer) |
| 17 | + * @property {number} [workerId] - Worker process ID |
| 18 | + */ |
| 19 | + |
| 20 | +/** |
| 21 | + * @typedef {Object} LogEntry |
| 22 | + * @property {string} timestamp - ISO timestamp |
| 23 | + * @property {string} level - Log level (info, warn, error, debug) |
| 24 | + * @property {string} phase - Processing phase (clone, enrich, analyze) |
| 25 | + * @property {string} message - Log message |
| 26 | + * @property {Object} [data] - Additional structured data |
| 27 | + */ |
| 28 | + |
| 29 | +/** |
| 30 | + * Create a module-specific logger that writes to file |
| 31 | + * |
| 32 | + * @param {ModuleLoggerOptions} options |
| 33 | + * @returns {Promise<ModuleLogger>} |
| 34 | + */ |
| 35 | +export async function createModuleLogger(options) { |
| 36 | + const { projectRoot, runId, moduleId, workerId } = options; |
| 37 | + |
| 38 | + // Create logs directory structure: logs/{runId}/modules/ |
| 39 | + const logsDir = path.join(projectRoot, "logs", runId, "modules"); |
| 40 | + await ensureDirectory(logsDir); |
| 41 | + |
| 42 | + // Sanitize module ID for filename (replace special chars) |
| 43 | + const safeModuleId = moduleId.replace(/[^a-zA-Z0-9_-]/gu, "_"); |
| 44 | + const logFileName = workerId |
| 45 | + ? `${safeModuleId}.worker-${workerId}.log` |
| 46 | + : `${safeModuleId}.log`; |
| 47 | + |
| 48 | + const logFilePath = path.join(logsDir, logFileName); |
| 49 | + |
| 50 | + // Log buffer (written periodically and on close) |
| 51 | + const logBuffer = []; |
| 52 | + let closed = false; |
| 53 | + |
| 54 | + /** |
| 55 | + * Write buffered logs to file |
| 56 | + */ |
| 57 | + async function flush() { |
| 58 | + if (logBuffer.length === 0 || closed) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + const content = `${logBuffer.join("\n")}\n`; |
| 63 | + await fs.appendFile(logFilePath, content, "utf8"); |
| 64 | + logBuffer.length = 0; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Format log entry |
| 69 | + * @param {LogEntry} entry |
| 70 | + * @returns {string} |
| 71 | + */ |
| 72 | + function formatLogEntry(entry) { |
| 73 | + const { timestamp, level, phase, message, data } = entry; |
| 74 | + const parts = [`[${timestamp}]`, `[${level.toUpperCase()}]`, `[${phase}]`, message]; |
| 75 | + |
| 76 | + if (data && Object.keys(data).length > 0) { |
| 77 | + parts.push(JSON.stringify(data)); |
| 78 | + } |
| 79 | + |
| 80 | + return parts.join(" "); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Add log entry |
| 85 | + * @param {string} level |
| 86 | + * @param {string} phase |
| 87 | + * @param {string} message |
| 88 | + * @param {Object} [data] |
| 89 | + */ |
| 90 | + async function log(level, phase, message, data) { |
| 91 | + if (closed) { |
| 92 | + return; |
| 93 | + } |
| 94 | + |
| 95 | + const entry = { |
| 96 | + timestamp: new Date().toISOString(), |
| 97 | + level, |
| 98 | + phase, |
| 99 | + message, |
| 100 | + ...(data && { data }) |
| 101 | + }; |
| 102 | + |
| 103 | + logBuffer.push(formatLogEntry(entry)); |
| 104 | + |
| 105 | + // Auto-flush on error or if buffer is large |
| 106 | + if (level === "error" || logBuffer.length >= 100) { |
| 107 | + await flush(); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Close logger and flush remaining logs |
| 113 | + */ |
| 114 | + async function close() { |
| 115 | + if (closed) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + await flush(); |
| 120 | + closed = true; |
| 121 | + } |
| 122 | + |
| 123 | + return { |
| 124 | + /** |
| 125 | + * Log info message |
| 126 | + * @param {string} phase |
| 127 | + * @param {string} message |
| 128 | + * @param {Object} [data] |
| 129 | + */ |
| 130 | + info: (phase, message, data) => log("info", phase, message, data), |
| 131 | + |
| 132 | + /** |
| 133 | + * Log warning message |
| 134 | + * @param {string} phase |
| 135 | + * @param {string} message |
| 136 | + * @param {Object} [data] |
| 137 | + */ |
| 138 | + warn: (phase, message, data) => log("warn", phase, message, data), |
| 139 | + |
| 140 | + /** |
| 141 | + * Log error message |
| 142 | + * @param {string} phase |
| 143 | + * @param {string} message |
| 144 | + * @param {Object} [data] |
| 145 | + */ |
| 146 | + error: (phase, message, data) => log("error", phase, message, data), |
| 147 | + |
| 148 | + /** |
| 149 | + * Log debug message |
| 150 | + * @param {string} phase |
| 151 | + * @param {string} message |
| 152 | + * @param {Object} [data] |
| 153 | + */ |
| 154 | + debug: (phase, message, data) => log("debug", phase, message, data), |
| 155 | + |
| 156 | + /** |
| 157 | + * Flush logs to file |
| 158 | + */ |
| 159 | + flush, |
| 160 | + |
| 161 | + /** |
| 162 | + * Close logger and flush |
| 163 | + */ |
| 164 | + close, |
| 165 | + |
| 166 | + /** |
| 167 | + * Get log file path |
| 168 | + */ |
| 169 | + getLogFilePath: () => logFilePath |
| 170 | + }; |
| 171 | +} |
| 172 | + |
| 173 | +/** |
| 174 | + * @typedef {Object} ModuleLogger |
| 175 | + * @property {(phase: string, message: string, data?: Object) => Promise<void>} info |
| 176 | + * @property {(phase: string, message: string, data?: Object) => Promise<void>} warn |
| 177 | + * @property {(phase: string, message: string, data?: Object) => Promise<void>} error |
| 178 | + * @property {(phase: string, message: string, data?: Object) => Promise<void>} debug |
| 179 | + * @property {() => Promise<void>} flush |
| 180 | + * @property {() => Promise<void>} close |
| 181 | + * @property {() => string} getLogFilePath |
| 182 | + */ |
0 commit comments