48

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' });
Jon
  • 2,456
  • 21
  • 28
Sparky1
  • 3,335
  • 6
  • 26
  • 29
  • 1
    +1 - good question! As a pure guess maybe write your own transport file logger that takes options on how to log (similar to IIS and other web servers)? – bryanmac Jul 09 '12 at 22:24

6 Answers6

31

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

indexzero
  • 1,024
  • 9
  • 5
  • 5
    This transport has been added to its own npm module now: https://github.com/winstonjs/winston-daily-rotate-file – Mike Stumpf Dec 04 '15 at 12:58
25

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
        })
    ]
});
Muki Buki
  • 259
  • 3
  • 2
  • 4
    It changed: https://github.com/winstonjs/winston/blob/d4fdbadc2f4ab8408261497a116ef80e0f9475a0/CHANGELOG.md#user-content-v200--2015-10-29 – lastboy Aug 01 '16 at 11:59
  • TL;DR: it's in a separate module now: https://www.npmjs.com/package/winston-daily-rotate-file – Jane Panda Nov 06 '22 at 12:33
7

You can use the following code to rotate the log files daily:

var winston = require('winston');
require('winston-daily-rotate-file');
var transport = new (winston.transports.DailyRotateFile)({
    filename: './log',
    datePattern: 'yyyy-MM-dd.',
    prepend: true,
    level: 'info'
});
var logger = new (winston.createLogger)({
    transports: [
      transport
    ]
});
logger.info('Hello World!');
Community
  • 1
  • 1
izhar ahmad
  • 81
  • 1
  • 2
3

According to the author of winston-filerotatedate it is a:

File transport for winston that allows the log files to be rotated depending on size and time.

The File transport accepts a filename via the 'filename' option and uses that file as the primary logging target. Should the file grow past 'maxsize' bytes then the current log file is renamed and a new primary log tile is created. The name of the renamed log file is formated as such 'basenameYYYYMMDD[a-z].bak'.

Available options are:

  • level: Level of messages that this transport should log.
  • silent: Boolean flag indicating whether to suppress output.
  • timestamp: Boolean flag indicating if we should prepend output with timestamps (default true). If function is specified, its return value will be used instead of timestamps.
  • filename: The filename of the logfile to write output to.
  • dirname: The folder the logfile will be created in.
  • maxsize: Max size in bytes of the logfile, if the size is exceeded then a new file is created.
  • json: If true, messages will be logged as JSON (default true).
DanArl
  • 1,113
  • 8
  • 10
2

Other people have already given good answers for this problem. But for those people looking for an example of how to do this in more modern JavaScript syntax(ES6 and beyond) using winston-daily-rotate-file, the following code should help:

const { createLogger, transports } = require('winston');
require('winston-daily-rotate-file');

const logger = createLogger({
    level: 'info',
    transports: [
      new transports.DailyRotateFile({
         filename: 'info-%DATE%.log',
         datePattern: 'YYYY-MM-DD',
         zippedArchive: true,
         level: 'info'
      })
    ]
});

logger.log('info', 'Test Log Message', { anything: 'This is metadata' });
Jon
  • 2,456
  • 21
  • 28
0

As of Dec 18, 2012, this feature is now available in Winston (see https://github.com/flatiron/winston/pull/205)

Pejvan
  • 772
  • 5
  • 8