node.js 温斯顿:如何轮换日志

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11403953/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:00:35  来源:igfitidea点击:

Winston: how to rotate logs

node.jsflatiron.js

提问by Sparky1

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?

使用 Winston 处理 node.js 的日志记录时如何轮换日志。也就是说,如何为应用程序运行的每一天创建一个新文件?

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

采纳答案by indexzero

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.

每天登录一个新文件目前是一个开放的功能请求:https: //github.com/flatiron/winston/issues/10。很想看到有人实施它。

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

  1. 文件传输接受 maxsize 选项,当日志文件超过特定大小(以字节为单位)时,该选项将旋转日志文件。

  2. 还有一个带有新传输的公开拉取请求,我还没有机会真正深入研究“fileRotate”,它似乎是这种基于日期的轮换:https: //github.com/flatiron/winston /拉/120/文件

回答by Muki Buki

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

该功能存在,我们正在生产中使用它,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
        })
    ]
});

回答by izhar ahmad

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

回答by DanArl

According to the author of winston-filerotatedateit is a:

根据winston-filerotatedate的作者,它是一个:

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).

winston 的文件传输,允许根据大小和时间轮换日志文件。

文件传输通过 'filename' 选项接受文件名,并将该文件用作主要的日志记录目标。如果文件增长超过 'maxsize' 字节,则重命名当前日志文件并创建新的主日志磁贴。重命名的日志文件的名称格式为“basenameYYYYMMDD[az].bak”。

可用选项有:

  • level:此传输应记录的消息级别。
  • 静音:布尔标志,指示是否抑制输出。
  • 时间戳:布尔标志,指示我们是否应该在输出前加上时间戳(默认为 true)。如果指定了函数,则将使用其返回值而不是时间戳。
  • 文件名:要写入输出的日志文件的文件名。
  • dirname:将在其中创建日志文件的文件夹。
  • maxsize:日志文件的最大字节数,如果超过大小,则创建一个新文件。
  • json:如果为 true,消息将被记录为 JSON(默认为 true)。

回答by Pejvan

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

截至 2012 年 12 月 18 日,此功能现已在 Winston 中可用(请参阅https://github.com/flatiron/winston/pull/205