story-weaver-ai
Version:
A narrative development system for AI-driven storytelling with Jungian psychology
129 lines (114 loc) • 3.23 kB
JavaScript
/**
* Story Weaver MCP Server - Logger
*
* @author Sean Pavlak
* @github https://github.com/seanpavlak/cursor-story-master
*
* Custom logger for the Story Weaver MCP server.
*/
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
// Constants
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Get log level from environment variable or use default
const LOG_LEVEL = process.env.LOG_LEVEL || "info";
const DEBUG = process.env.DEBUG === "true" || process.env.DEBUG === "1";
// Log level priorities
const LOG_LEVELS = {
debug: 0,
info: 1,
warn: 2,
error: 3,
};
// ANSI color codes
const COLORS = {
reset: "\x1b[0m",
debug: "\x1b[36m", // Cyan
info: "\x1b[32m", // Green
warn: "\x1b[33m", // Yellow
error: "\x1b[31m", // Red
};
/**
* Centralized logger for the Story Weaver MCP server
*/
class Logger {
constructor() {
this.logLevel = LOG_LEVELS[LOG_LEVEL] || LOG_LEVELS.info;
this.debug = this.debug.bind(this);
this.info = this.info.bind(this);
this.warn = this.warn.bind(this);
this.error = this.error.bind(this);
// Initialize debug log file if DEBUG is enabled
if (DEBUG) {
const logDir = path.join(__dirname, "../../logs");
if (!fs.existsSync(logDir)) {
fs.mkdirSync(logDir, { recursive: true });
}
this.logFilePath = path.join(logDir, "story-weaver-mcp-debug.log");
// Log startup entry
const timestamp = new Date().toISOString();
fs.appendFileSync(
this.logFilePath,
`\n[${timestamp}] Story Weaver MCP Server Debug Log Started\n`
);
}
}
/**
* Write a message to the log file if DEBUG is enabled
* @param {string} level - Log level
* @param {string} message - Log message
*/
_writeToFile(level, message) {
if (DEBUG && this.logFilePath) {
const timestamp = new Date().toISOString();
fs.appendFileSync(
this.logFilePath,
`[${timestamp}] [${level.toUpperCase()}] ${message}\n`
);
}
}
/**
* Log a debug message
* @param {string} message - Debug message
*/
debug(message) {
if (this.logLevel <= LOG_LEVELS.debug) {
console.log(`${COLORS.debug}[DEBUG]${COLORS.reset} ${message}`);
this._writeToFile("debug", message);
}
}
/**
* Log an info message
* @param {string} message - Info message
*/
info(message) {
if (this.logLevel <= LOG_LEVELS.info) {
console.log(`${COLORS.info}[INFO]${COLORS.reset} ${message}`);
this._writeToFile("info", message);
}
}
/**
* Log a warning message
* @param {string} message - Warning message
*/
warn(message) {
if (this.logLevel <= LOG_LEVELS.warn) {
console.log(`${COLORS.warn}[WARN]${COLORS.reset} ${message}`);
this._writeToFile("warn", message);
}
}
/**
* Log an error message
* @param {string} message - Error message
*/
error(message) {
if (this.logLevel <= LOG_LEVELS.error) {
console.error(`${COLORS.error}[ERROR]${COLORS.reset} ${message}`);
this._writeToFile("error", message);
}
}
}
// Export a singleton instance
export default new Logger();