logger.ts•1.16 kB
/**
* Logging utility using Winston
*/
import winston from 'winston';
import config from '../config/index.js';
const logFormat = winston.format.combine(
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
winston.format.errors({ stack: true }),
winston.format.splat(),
config.logging.format === 'json'
? winston.format.json()
: winston.format.printf(({ level, message, timestamp, ...metadata }) => {
let msg = `${timestamp} [${level.toUpperCase()}]: ${message}`;
if (Object.keys(metadata).length > 0) {
msg += ` ${JSON.stringify(metadata)}`;
}
return msg;
})
);
export const logger = winston.createLogger({
level: config.logging.level,
format: logFormat,
transports: [
new winston.transports.Console({
stderrLevels: ['error'],
}),
],
});
// Add file transport in production
if (config.environment === 'production') {
logger.add(
new winston.transports.File({
filename: 'logs/error.log',
level: 'error',
})
);
logger.add(
new winston.transports.File({
filename: 'logs/combined.log',
})
);
}
export default logger;