WinstonJS将时间戳集成到FileTransport的日志文件名中

时间:2018-07-17 08:37:38

标签: node.js logging winston

我正在使用winstonjs 3.0.0来生成日志文件,当前正在使用文件传输来生成日志文件。

const file = new winston.trasnports.File({
...
filename: 'xxx.log',
maxSize: 1024
...
}) 

使用下面的代码,我得到名称为'xxx.log', 'xxx1.log'的日志文件。 创建文件时,我想获取带有日期信息的文件名:'YYYY-MM-DD-xxx.log'。我考虑过'winston-daily-rotate-file',但我不希望日志随时间旋转。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在文件名的开头附加日期,如下所示:

let mydate = new Date();
let newFilename = mydate.getFullYear() + "-" + mydate.getMonth() + "-" + mydate.getDate() + "-" + "xxx.log";
const file = new winston.trasnports.File({
...
filename: newFilename,
maxSize: 1024
...
})
相关问题