Winston: how to rotate logs


Question

How can I rotate logs when using Winston to handle logging for node.js. That is, how can I create a new file for each day the app runs?

    var logger = new (winston.Logger)({
       transports: [
          new (winston.transports.Console)(),
          new (winston.transports.File)({ filename: '2012-07-09.log' })
      ]
});

logger.log('info', 'Test Log Message', { anything: 'This is metadata' });
1
37
7/10/2012 3:21:57 AM

Accepted Answer

winston author and maintainer here.

Logging to a new file everyday is currently an open feature request: https://github.com/flatiron/winston/issues/10. Would love to see someone implement it.

That said, there are other options:

  1. The File transport accepts a maxsize option which will rotate the logfile when it exceeds a certain size in bytes.

  2. There is also an open pull-request with a new transport I haven't had a chance to really dig into "fileRotate", which it seems does this kind of date-based rotation: https://github.com/flatiron/winston/pull/120/files

24
7/10/2012 8:04:56 AM

The feature is present and we are using it in production, winston.transports.DailyRotateFile:

var timeFormatFn = function() {
    'use strict';
    return moment().format(cfg.timeFormat);
};

var logger = new(winston.Logger)({
    exitOnError: false,
    transports: [
        new(winston.transports.DailyRotateFile)({
            filename: cfg.appLogName,
            dirname: __dirname + '/../' + cfg.logsDirectory,
            datePattern: cfg.rollingDatePattern,
            timestamp: timeFormatFn
        }),
        new(winston.transports.Console)({
            colorize: true,
            timestamp: timeFormatFn
        })
    ]
});

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon