Log Rotation

For managing log file sizes and rotation, you can use the winston-daily-rotate-file transport. Install it first:

npm install winston-daily-rotate-file

Then configure it like so:

const winston = require('winston');
require('winston-daily-rotate-file');

const transport = new winston.transports.DailyRotateFile({
  filename: 'application-%DATE%.log',
  datePattern: 'YYYY-MM-DD',
  maxSize: '20m',
  maxFiles: '14d',
  level: 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  )
});

const logger = winston.createLogger({
  level: 'info',
  transports: [transport]
});

logger.info('This message will be logged to a daily rotated file');

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *