Winston Daily Rotate File Transport
Rotate application logs by day and size with winston-daily-rotate-file, keeping bounded history, zipped archives, and separate error streams.
Code
import winston from 'winston';
import 'winston-daily-rotate-file';
const transport = new winston.transports.DailyRotateFile({
dirname: 'logs',
filename: 'app-%DATE%.log',
datePattern: 'YYYY-MM-DD',
maxSize: '50m',
maxFiles: '30d',
zippedArchive: true,
auditFile: 'logs/audit.json',
level: 'info',
});
const errorTransport = new winston.transports.DailyRotateFile({
dirname: 'logs',
filename: 'error-%DATE%.log',
datePattern: 'YYYY-MM-DD',
maxSize: '20m',
maxFiles: '90d',
zippedArchive: true,
level: 'error',
});
export const logger = winston.createLogger({
level: 'info',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.errors({ stack: true }),
winston.format.json(),
),
transports: [transport, errorTransport],
});
logger.info('rotation policy active');
logger.error('payment webhook failed', { attempt: 3 });Report a problem with this exampleOpens a pre-filled issue on GitHub.