Winston: how to rotate logs

前端 未结 5 1475
傲寒
傲寒 2020-12-08 04:13

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          


        
相关标签:
5条回答
  • 2020-12-08 04:37

    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).
    0 讨论(0)
  • 2020-12-08 04:42

    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!');
    
    0 讨论(0)
  • 2020-12-08 04:45

    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
            })
        ]
    });
    
    0 讨论(0)
  • 2020-12-08 04:56

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

    0 讨论(0)
  • 2020-12-08 04:58

    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

    0 讨论(0)
提交回复
热议问题