import * as winston from 'winston';
import * as path from 'path';
export class Logger {
private static instance: Logger;
private logger: winston.Logger;
constructor(context: string) {
this.logger = winston.createLogger({
level: process.env.LOG_LEVEL || 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.splat(),
winston.format.json()
),
defaultMeta: { service: 'mcp-console', context },
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.colorize(),
winston.format.simple()
)
})
]
});
if (process.env.NODE_ENV === 'production') {
this.logger.add(new winston.transports.File({
filename: path.join(process.cwd(), 'logs', 'error.log'),
level: 'error'
}));
this.logger.add(new winston.transports.File({
filename: path.join(process.cwd(), 'logs', 'combined.log')
}));
}
}
info(message: string, meta?: any) {
this.logger.info(message, meta);
}
error(message: string, meta?: any) {
this.logger.error(message, meta);
}
warn(message: string, meta?: any) {
this.logger.warn(message, meta);
}
debug(message: string, meta?: any) {
this.logger.debug(message, meta);
}
getWinstonLogger(): winston.Logger {
return this.logger;
}
static getInstance(context: string = 'default'): Logger {
if (!Logger.instance) {
Logger.instance = new Logger(context);
}
return Logger.instance;
}
}