super-shell-mcp
Version:
MCP server for executing shell commands across multiple platforms
100 lines • 2.88 kB
JavaScript
import * as fs from 'fs';
import * as path from 'path';
/**
* Simple logging utility that writes to a file
*/
export class Logger {
logFile;
enabled;
fileStream = null;
/**
* Create a new logger
* @param logFile Path to the log file
* @param enabled Whether logging is enabled
*/
constructor(logFile, enabled = true) {
this.logFile = logFile;
this.enabled = enabled;
if (this.enabled) {
// Create the directory if it doesn't exist
const logDir = path.dirname(this.logFile);
console.error(`Creating log directory: ${logDir}`);
try {
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
}
catch (error) {
console.error(`Error creating log directory: ${error}`);
// Fall back to a directory we know exists
this.logFile = './super-shell-mcp.log';
console.error(`Falling back to log file: ${this.logFile}`);
}
// Create or truncate the log file
this.fileStream = fs.createWriteStream(this.logFile, { flags: 'w' });
// Write a header to the log file
this.log('INFO', `Logging started at ${new Date().toISOString()}`);
}
}
/**
* Log a message
* @param level Log level (INFO, DEBUG, ERROR, etc.)
* @param message Message to log
*/
log(level, message) {
if (!this.enabled || !this.fileStream) {
return;
}
const timestamp = new Date().toISOString();
const logMessage = `[${timestamp}] [${level}] ${message}\n`;
this.fileStream.write(logMessage);
}
/**
* Log an info message
* @param message Message to log
*/
info(message) {
this.log('INFO', message);
}
/**
* Log a debug message
* @param message Message to log
*/
debug(message) {
this.log('DEBUG', message);
}
/**
* Log an error message
* @param message Message to log
*/
error(message) {
this.log('ERROR', message);
}
/**
* Close the logger
*/
close() {
if (this.fileStream) {
this.fileStream.end();
this.fileStream = null;
}
}
}
// Create a singleton logger instance
let loggerInstance = null;
/**
* Get the logger instance
* @param logFile Path to the log file
* @param enabled Whether logging is enabled
* @returns Logger instance
*/
export function getLogger(logFile, enabled) {
if (!loggerInstance && logFile) {
loggerInstance = new Logger(logFile, enabled);
}
if (!loggerInstance) {
throw new Error('Logger not initialized');
}
return loggerInstance;
}
//# sourceMappingURL=logger.js.map