66 lines
1.7 KiB
TypeScript
66 lines
1.7 KiB
TypeScript
/**
|
|
* Logger Utility
|
|
* Winston-based structured logging
|
|
*/
|
|
|
|
import winston from 'winston';
|
|
|
|
export class Logger {
|
|
private logger: winston.Logger;
|
|
private context: string;
|
|
|
|
constructor(context: string) {
|
|
this.context = context;
|
|
|
|
this.logger = winston.createLogger({
|
|
level: process.env.LOG_LEVEL || 'info',
|
|
format: winston.format.combine(
|
|
winston.format.timestamp(),
|
|
winston.format.errors({ stack: true }),
|
|
winston.format.json()
|
|
),
|
|
defaultMeta: { service: 'aimds-gateway', context },
|
|
transports: [
|
|
new winston.transports.Console({
|
|
format: winston.format.combine(
|
|
winston.format.colorize(),
|
|
winston.format.printf(({ timestamp, level, message, context, ...meta }) => {
|
|
const metaStr = Object.keys(meta).length > 0
|
|
? JSON.stringify(meta)
|
|
: '';
|
|
return `${timestamp} [${context}] ${level}: ${message} ${metaStr}`;
|
|
})
|
|
)
|
|
}),
|
|
new winston.transports.File({
|
|
filename: 'logs/error.log',
|
|
level: 'error'
|
|
}),
|
|
new winston.transports.File({
|
|
filename: 'logs/combined.log'
|
|
})
|
|
]
|
|
});
|
|
}
|
|
|
|
debug(message: string, meta?: Record<string, unknown>): void {
|
|
this.logger.debug(message, meta);
|
|
}
|
|
|
|
info(message: string, meta?: Record<string, unknown>): void {
|
|
this.logger.info(message, meta);
|
|
}
|
|
|
|
warn(message: string, meta?: Record<string, unknown>): void {
|
|
this.logger.warn(message, meta);
|
|
}
|
|
|
|
error(message: string, meta?: Record<string, unknown>): void {
|
|
this.logger.error(message, meta);
|
|
}
|
|
|
|
child(childContext: string): Logger {
|
|
return new Logger(`${this.context}:${childContext}`);
|
|
}
|
|
}
|