Logger.js•4.01 kB
"use strict";
/**
* Logger utility for the MCP Prompt Enhancer
* Provides different logging levels and formats for development and production
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.logger = exports.Logger = exports.LogLevel = void 0;
var LogLevel;
(function (LogLevel) {
LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
LogLevel[LogLevel["INFO"] = 1] = "INFO";
LogLevel[LogLevel["WARN"] = 2] = "WARN";
LogLevel[LogLevel["ERROR"] = 3] = "ERROR";
LogLevel[LogLevel["NONE"] = 4] = "NONE";
})(LogLevel || (exports.LogLevel = LogLevel = {}));
class Logger {
constructor(config = {}) {
this.config = {
level: config.level ?? (process.env.NODE_ENV === 'production' ? LogLevel.INFO : LogLevel.DEBUG),
isDevelopment: config.isDevelopment ?? process.env.NODE_ENV !== 'production',
showTimestamp: config.showTimestamp ?? true
};
}
/**
* Get the singleton logger instance
*/
static getInstance(config) {
if (!Logger.instance) {
Logger.instance = new Logger(config);
}
return Logger.instance;
}
/**
* Reconfigure the logger
*/
configure(config) {
this.config = { ...this.config, ...config };
}
/**
* Get current log level as string
*/
getLogLevelName() {
return LogLevel[this.config.level];
}
/**
* Format a log message with optional timestamp
*/
formatMessage(message) {
if (this.config.showTimestamp) {
const timestamp = new Date().toISOString();
return `[${timestamp}] ${message}`;
}
return message;
}
/**
* Log a debug message
*/
debug(message, ...args) {
if (this.config.level <= LogLevel.DEBUG) {
const formattedMessage = this.formatMessage(`DEBUG: ${message}`);
console.debug(formattedMessage, ...args);
}
}
/**
* Log an info message
*/
info(message, ...args) {
if (this.config.level <= LogLevel.INFO) {
const formattedMessage = this.formatMessage(`INFO: ${message}`);
console.info(formattedMessage, ...args);
}
}
/**
* Log a warning message
*/
warn(message, ...args) {
if (this.config.level <= LogLevel.WARN) {
const formattedMessage = this.formatMessage(`WARN: ${message}`);
console.warn(formattedMessage, ...args);
}
}
/**
* Log an error message
*/
error(message, error, ...args) {
if (this.config.level <= LogLevel.ERROR) {
const formattedMessage = this.formatMessage(`ERROR: ${message}`);
if (error) {
console.error(formattedMessage, error, ...args);
if (this.config.isDevelopment) {
console.error(error.stack);
}
}
else {
console.error(formattedMessage, ...args);
}
}
}
/**
* Log with a custom level and prefix
*/
log(level, prefix, message, ...args) {
if (level >= this.config.level) {
const formattedMessage = this.formatMessage(`${prefix}: ${message}`);
console.log(formattedMessage, ...args);
}
}
/**
* Create a child logger with a specific component prefix
*/
createChildLogger(component) {
return {
debug: (message, ...args) => this.debug(`[${component}] ${message}`, ...args),
info: (message, ...args) => this.info(`[${component}] ${message}`, ...args),
warn: (message, ...args) => this.warn(`[${component}] ${message}`, ...args),
error: (message, error, ...args) => this.error(`[${component}] ${message}`, error, ...args),
};
}
}
exports.Logger = Logger;
// Default logger instance
exports.logger = Logger.getInstance();
exports.default = exports.logger;
//# sourceMappingURL=Logger.js.map