Winston: how to rotate logs

2019-01-22 01:27发布

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' });

5条回答
\"骚年 ilove
2楼-- · 2019-01-22 02:10

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

查看更多
姐就是有狂的资本
3楼-- · 2019-01-22 02:16

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).
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-22 02:19

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.Logger)({
    transports: [
      transport
    ]
});
logger.info('Hello World!');
查看更多
我命由我不由天
5楼-- · 2019-01-22 02:31

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
        })
    ]
});
查看更多
Explosion°爆炸
6楼-- · 2019-01-22 02:32

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

查看更多
登录 后发表回答