37 lines
1.1 KiB
JavaScript
37 lines
1.1 KiB
JavaScript
import winston from 'winston';
|
|
import path from 'path';
|
|
import { fileURLToPath } from 'url';
|
|
|
|
const __filename = fileURLToPath(import.meta.url);
|
|
const __dirname = path.dirname(__filename);
|
|
|
|
export function createLogger(service) {
|
|
// Create logs directory relative to the project root (two levels up from utils)
|
|
const logsDir = path.join(__dirname, '../../logs');
|
|
|
|
return winston.createLogger({
|
|
level: process.env.LOG_LEVEL || 'info',
|
|
format: winston.format.combine(
|
|
winston.format.timestamp(),
|
|
winston.format.json()
|
|
),
|
|
defaultMeta: { service },
|
|
transports: [
|
|
// Write all logs to console
|
|
new winston.transports.Console({
|
|
format: winston.format.combine(
|
|
winston.format.colorize(),
|
|
winston.format.simple()
|
|
)
|
|
}),
|
|
// Write all logs to service-specific files
|
|
new winston.transports.File({
|
|
filename: path.join(logsDir, `${service}-error.log`),
|
|
level: 'error'
|
|
}),
|
|
new winston.transports.File({
|
|
filename: path.join(logsDir, `${service}-combined.log`)
|
|
})
|
|
]
|
|
});
|
|
}
|