ckriti/HuggingClaw
0
1/**2 * Structured Logger for OpenClaw3 * Provides consistent JSON logging for HF Spaces4 */5 6const fs = require('fs');7const path = require('path');8 9// Ensure logs directory exists10const LOG_DIR = path.join(process.env.HOME || '/home/node', 'logs');11if (!fs.existsSync(LOG_DIR)) {12 try {13 fs.mkdirSync(LOG_DIR, { recursive: true });14 } catch (e) {15 // Ignore if we can't create it (might be read-only or race condition)16 }17}18 19const LOG_FILE = path.join(LOG_DIR, 'app.json.log');20 21class Logger {22 constructor(moduleName) {23 this.module = moduleName;24 }25 26 _log(level, message, data = {}) {27 const entry = {28 timestamp: new Date().toISOString(),29 level: level.toUpperCase(),30 module: this.module,31 message,32 ...data33 };34 35 const jsonLine = JSON.stringify(entry);36 37 // Write to stdout for HF Logs visibility38 console.log(jsonLine);39 40 // Also append to local file for persistence within container life41 try {42 fs.appendFileSync(LOG_FILE, jsonLine + '\n');43 } catch (e) {44 // Fallback if file write fails45 console.error(`[LOGGER_FAIL] Could not write to log file: ${e.message}`);46 }47 }48 49 info(message, data) { this._log('INFO', message, data); }50 warn(message, data) { this._log('WARN', message, data); }51 error(message, data) { this._log('ERROR', message, data); }52 debug(message, data) { this._log('DEBUG', message, data); }53 54 // Special method for critical state changes55 state(stateName, previousState, newState, data) {56 this._log('STATE_CHANGE', `State changed: ${stateName}`, {57 previousState,58 newState,59 ...data60 });61 }62}63 64module.exports = (moduleName) => new Logger(moduleName);65 