Logger.ts•4.06 kB
/**
* Logger utility for the MCP Prompt Enhancer
* Provides different logging levels and formats for development and production
*/
export enum LogLevel {
DEBUG = 0,
INFO = 1,
WARN = 2,
ERROR = 3,
NONE = 4
}
export interface LoggerConfig {
level: LogLevel;
isDevelopment: boolean;
showTimestamp: boolean;
}
export class Logger {
private static instance: Logger;
private config: LoggerConfig;
private constructor(config: Partial<LoggerConfig> = {}) {
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
*/
public static getInstance(config?: Partial<LoggerConfig>): Logger {
if (!Logger.instance) {
Logger.instance = new Logger(config);
}
return Logger.instance;
}
/**
* Reconfigure the logger
*/
public configure(config: Partial<LoggerConfig>): void {
this.config = { ...this.config, ...config };
}
/**
* Get current log level as string
*/
public getLogLevelName(): string {
return LogLevel[this.config.level];
}
/**
* Format a log message with optional timestamp
*/
private formatMessage(message: string): string {
if (this.config.showTimestamp) {
const timestamp = new Date().toISOString();
return `[${timestamp}] ${message}`;
}
return message;
}
/**
* Log a debug message
*/
public debug(message: string, ...args: any[]): void {
if (this.config.level <= LogLevel.DEBUG) {
const formattedMessage = this.formatMessage(`DEBUG: ${message}`);
console.debug(formattedMessage, ...args);
}
}
/**
* Log an info message
*/
public info(message: string, ...args: any[]): void {
if (this.config.level <= LogLevel.INFO) {
const formattedMessage = this.formatMessage(`INFO: ${message}`);
console.info(formattedMessage, ...args);
}
}
/**
* Log a warning message
*/
public warn(message: string, ...args: any[]): void {
if (this.config.level <= LogLevel.WARN) {
const formattedMessage = this.formatMessage(`WARN: ${message}`);
console.warn(formattedMessage, ...args);
}
}
/**
* Log an error message
*/
public error(message: string, error?: Error, ...args: any[]): void {
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
*/
public log(level: LogLevel, prefix: string, message: string, ...args: any[]): void {
if (level >= this.config.level) {
const formattedMessage = this.formatMessage(`${prefix}: ${message}`);
console.log(formattedMessage, ...args);
}
}
/**
* Create a child logger with a specific component prefix
*/
public createChildLogger(component: string): {
debug: (message: string, ...args: any[]) => void;
info: (message: string, ...args: any[]) => void;
warn: (message: string, ...args: any[]) => void;
error: (message: string, error?: Error, ...args: any[]) => void;
} {
return {
debug: (message: string, ...args: any[]) =>
this.debug(`[${component}] ${message}`, ...args),
info: (message: string, ...args: any[]) =>
this.info(`[${component}] ${message}`, ...args),
warn: (message: string, ...args: any[]) =>
this.warn(`[${component}] ${message}`, ...args),
error: (message: string, error?: Error, ...args: any[]) =>
this.error(`[${component}] ${message}`, error, ...args),
};
}
}
// Default logger instance
export const logger = Logger.getInstance();
export default logger;