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.

JavascriptNodeResilience

Code

javascriptsnippet
36 lines
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 });

What the code does

Module-level bindings hold the data — transport, errorTransport — with no function scope in between.

Emits through the .info, .error level methods with structured payloads, so downstream transports can route by level and field.

Watch out for

  • Plan for an install step: winston is a real dependency, resolved by the package manager.

Applying it

The main signature reads export const logger =

logger is the public entry point; everything else in the file exists to support it.

Interactive Tools for This Topic

FAQ

Does this require external packages?
Yes — `winston`. Add it to your project before running the snippet.