claude-flow-novice
Version:
Claude Flow Novice - Advanced orchestration platform for multi-agent AI workflows with CFN Loop architecture Includes Local RuVector Accelerator and all CFN skills for complete functionality.
146 lines (145 loc) • 4.38 kB
JavaScript
/**
* Structured Logging Utility
*
* Provides structured JSON logging for consistent log output across the application.
* Part of Task 0.5: Implementation Tooling & Utilities (Foundation)
*
* Usage:
* const logger = createLogger('database-service');
* logger.info('Connection established', { host: 'localhost', port: 5432 });
*/ import * as fs from 'fs';
import * as path from 'path';
/**
* Log levels in order of severity
*/ export var LogLevel = /*#__PURE__*/ function(LogLevel) {
LogLevel["DEBUG"] = "debug";
LogLevel["INFO"] = "info";
LogLevel["WARN"] = "warn";
LogLevel["ERROR"] = "error";
return LogLevel;
}({});
/**
* Log level priorities for filtering
*/ const LOG_LEVEL_PRIORITY = {
["debug"]: 0,
["info"]: 1,
["warn"]: 2,
["error"]: 3
};
/**
* Create a structured logger instance
*
* @param context - Logger context (e.g., 'database-service', 'api-handler')
* @param options - Logger configuration options
* @returns Logger instance
*/ export function createLogger(context, options = {}) {
const { minLevel = "info", console: consoleOutput = true, filePath, pretty = false } = options;
let currentMinLevel = minLevel;
let fileStream = null;
// Initialize file stream if file path provided
if (filePath) {
const logDir = path.dirname(filePath);
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, {
recursive: true
});
}
fileStream = fs.createWriteStream(filePath, {
flags: 'a'
});
}
/**
* Write log entry to outputs
*/ function writeLog(entry) {
// Check if log level meets minimum threshold
if (LOG_LEVEL_PRIORITY[entry.level] < LOG_LEVEL_PRIORITY[currentMinLevel]) {
return;
}
const jsonString = pretty ? JSON.stringify(entry, null, 2) : JSON.stringify(entry);
// Console output
if (consoleOutput) {
switch(entry.level){
case "debug":
console.debug(jsonString);
break;
case "info":
console.info(jsonString);
break;
case "warn":
console.warn(jsonString);
break;
case "error":
console.error(jsonString);
break;
}
}
// File output
if (fileStream) {
fileStream.write(jsonString + '\n');
}
}
/**
* Create log entry
*/ function createLogEntry(level, message, metadata, error) {
const entry = {
timestamp: new Date().toISOString(),
level,
context,
message
};
if (metadata && Object.keys(metadata).length > 0) {
entry.metadata = metadata;
}
if (error) {
entry.error = {
name: error.name,
message: error.message,
stack: error.stack
};
}
return entry;
}
return {
debug (message, metadata) {
const entry = createLogEntry("debug", message, metadata);
writeLog(entry);
},
info (message, metadata) {
const entry = createLogEntry("info", message, metadata);
writeLog(entry);
},
warn (message, metadata) {
const entry = createLogEntry("warn", message, metadata);
writeLog(entry);
},
error (message, error, metadata) {
const entry = createLogEntry("error", message, metadata, error);
writeLog(entry);
},
setMinLevel (level) {
currentMinLevel = level;
}
};
}
/**
* Global logger instance (default context)
*/ let globalLogger = null;
/**
* Get or create global logger
*
* @param options - Logger configuration options
* @returns Global logger instance
*/ export function getGlobalLogger(options) {
if (!globalLogger) {
globalLogger = createLogger('global', options);
}
return globalLogger;
}
/**
* Set global logger instance
*
* @param logger - Logger instance to use globally
*/ export function setGlobalLogger(logger) {
globalLogger = logger;
}
//# sourceMappingURL=logging.js.map