57 lines
1.5 KiB
TypeScript
57 lines
1.5 KiB
TypeScript
import winston from 'winston';
|
|
import path from 'path';
|
|
|
|
const logDir = path.join(__dirname, '../../logs');
|
|
|
|
// Define log format
|
|
const logFormat = winston.format.combine(
|
|
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
|
winston.format.errors({ stack: true }),
|
|
winston.format.splat(),
|
|
winston.format.json()
|
|
);
|
|
|
|
// Console format for development
|
|
const consoleFormat = winston.format.combine(
|
|
winston.format.colorize(),
|
|
winston.format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
|
|
winston.format.printf(({ timestamp, level, message, ...meta }) => {
|
|
let metaString = '';
|
|
if (Object.keys(meta).length > 0) {
|
|
metaString = JSON.stringify(meta, null, 2);
|
|
}
|
|
return `${timestamp} [${level}]: ${message} ${metaString}`;
|
|
})
|
|
);
|
|
|
|
// Create the logger
|
|
const logger = winston.createLogger({
|
|
level: process.env.LOG_LEVEL || 'info',
|
|
format: logFormat,
|
|
defaultMeta: { service: 'feuerwehr-dashboard-api' },
|
|
transports: [
|
|
// Write all logs with importance level of 'error' or less to error.log
|
|
new winston.transports.File({
|
|
filename: path.join(logDir, 'error.log'),
|
|
level: 'error',
|
|
maxsize: 5242880, // 5MB
|
|
maxFiles: 5,
|
|
}),
|
|
// Write all logs to combined.log
|
|
new winston.transports.File({
|
|
filename: path.join(logDir, 'combined.log'),
|
|
maxsize: 5242880, // 5MB
|
|
maxFiles: 5,
|
|
}),
|
|
],
|
|
});
|
|
|
|
// Always log to console so errors are visible in `docker logs`
|
|
logger.add(
|
|
new winston.transports.Console({
|
|
format: consoleFormat,
|
|
})
|
|
);
|
|
|
|
export default logger;
|