ludus-mcp
Version:
MCP server for managing Ludus cybersecurity training environments through natural language commands
95 lines • 2.81 kB
JavaScript
import { FileLogger } from './fileLogger.js';
/**
* Logger utility for structured logging with different levels
*/
export class Logger {
constructor(context) {
this.context = context;
this.isDebugEnabled = process.env.LUDUS_DEBUG === 'true' || process.env.NODE_ENV === 'development';
// Always enable file logging for debugging purposes
this.isFileLoggingEnabled = true;
if (this.isFileLoggingEnabled) {
try {
this.fileLogger = new FileLogger('ludus-mcp');
}
catch (error) {
console.error('Failed to initialize file logging:', error);
}
}
}
/**
* Log debug messages (only in debug mode)
*/
debug(message, meta) {
if (this.isDebugEnabled) {
this.log('DEBUG', message, meta);
}
}
/**
* Log info messages
*/
info(message, meta) {
this.log('INFO', message, meta);
}
/**
* Log warning messages
*/
warn(message, meta) {
this.log('WARN', message, meta);
}
/**
* Log error messages
*/
error(message, error, meta) {
const errorMeta = error instanceof Error
? {
message: error.message,
stack: error.stack,
name: error.name,
...meta
}
: { error: String(error), ...meta };
this.log('ERROR', message, errorMeta);
}
/**
* Core logging method
*/
log(level, message, meta) {
const timestamp = new Date().toISOString();
const logEntry = {
timestamp,
level,
context: this.context,
message,
...(meta && Object.keys(meta).length > 0 ? { meta } : {}),
};
// Always write to file if enabled
if (this.fileLogger) {
this.fileLogger.log(level, this.context, message, meta);
}
// Output all logs to stderr to avoid interfering with MCP protocol on stdout
const output = console.error;
if (process.env.LUDUS_LOG_FORMAT === 'json') {
output(JSON.stringify(logEntry));
}
else {
const metaStr = meta && Object.keys(meta).length > 0
? ` ${JSON.stringify(meta)}`
: '';
output(`[${timestamp}] ${level} [${this.context}] ${message}${metaStr}`);
}
}
/**
* Get the file log path if file logging is enabled
*/
getLogPath() {
return this.fileLogger?.getLogPath() || null;
}
/**
* Get the log directory if file logging is enabled
*/
getLogDirectory() {
return this.fileLogger?.getLogDirectory() || null;
}
}
//# sourceMappingURL=logger.js.map