【发布时间】:2017-01-31 05:45:45
【问题描述】:
我正在为 Node 项目使用 winston-daily-rotate-file。如何使日志文件每周轮换一次?
【问题讨论】:
我正在为 Node 项目使用 winston-daily-rotate-file。如何使日志文件每周轮换一次?
【问题讨论】:
来自winston-daily-rotate-file的描述
我们可以看到 datePattern 的选项:“表示用于旋转的moment.js date format 的字符串。此字符串中使用的元字符将指示文件旋转的频率。例如,如果您的 datePattern 是只需“HH”,您最终会得到 24 个日志文件,这些文件每天都会被拾取并附加到。(默认值:“YYYY-MM-DD”)"
因此,要回答您的问题,让日志文件每周轮换的一种简单方法是设置 datePattern: 'YYYY-w',其中 w 是一年中的第几周:1 2 ... 52 53。
datePattern 的更多选项可以在moment.js date format找到
一个简单的用法如下所示。
const winston = require('winston');
const DailyRotateFile = require('winston-daily-rotate-file');
var winston = require('winston');
require('winston-daily-rotate-file');
var transport = new winston.transports.DailyRotateFile({
filename: 'application-%DATE%.log',
datePattern: 'YYYY-w'
});
transport.on('rotate', function(oldFilename, newFilename) {
logger.info({'message':'New file created!'});
});
var logger = winston.createLogger({
level: 'info',
transports: [
transport
]
});
logger.info('Hello World!');
因此,日志文件将命名为 application-year-week.log
例如。从 2020 年 7 月 6 日到 2020 年 7 月 12 日的一周是 2020 年的第 28 周。这意味着将为本周创建文件 application-2020-28.log,为下周创建 application-2020-29.log,等等。您可以计算一年中的第几周这个Week Number Calculator
【讨论】: