除第一天外,如何使用Winston每天轮换日志 [英] How to daily rotate logs using Winston except the first day

查看:250
本文介绍了除第一天外,如何使用Winston每天轮换日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

除了当前文件以外,我每天都需要轮换日志. 我正在使用 winston winston-daily-rotate-file 库.

I need to rotate the logs daily except the file for current day. I'm using winston and winston-daily-rotate-file libraries.

在以下示例中,仅在我第一次执行节点时生成了文件"info.log.2016-08-09".

In the following example, a file "info.log.2016-08-09" is generated just the first time I execute node.

但是,我确实需要生成文件"info.log",并且在这一天之后,应将其重命名为"info.log.2016-08-09",并为该文件创建一个新的"info.log"当前日期. 我确信这是其他应用程序中的正常行为.

But, I really need to generate the file "info.log", and after this day, should be renamed to "info.log.2016-08-09", and create a new "info.log" for the current day. I understant that this is the normal behaviour in other applications.

var logger = new (winston.Logger)({
  transports: [
    new dailyRotateFile(  
      {
        name: 'cronInfo',
        filename:  path.join(__dirname,"log", "info.log"),
      level: 'info',
       timestamp: function(){                
        return utils.formatDate(new Date(), "yyyy-mm-dd'T'HH:MM:ss.l'Z'")
      },
      formatter: function(options) {
          return  options.timestamp() +' ['+ options.level.toUpperCase() +'] '+ (undefined !== options.message ? options.message : '') +
               (options.meta && Object.keys(options.meta).length ? '\n\t'+ JSON.stringify(options.meta) : '' );          
      },
      json:false,
      datePattern:".yyyy-MM-dd"
    })
   ]
});
 

推荐答案

另一种没有数据边界问题的方法只是使用:

Another approach without data boundary issues would be just to use:

var logger = new (winston.Logger)({
  transports: [
    new dailyRotateFile(  
      {
        // your definition of rotate file
      })
   ]
});

然后为info.log创建一个链接到当前文件的符号链接.最后,您可以使用:

And then create a symlink for info.log that links to the current day's file. Finally you can use:

transport.on('rotate', function(oldFilename, newFilename) {
    fs.symlinkSync(newFilename, 'info.log');
});

在旋转时更新符号链接.

to update the symlink when it rotates.

如果您的日志文件特别大,这还将节省一些磁盘空间.

If your log files are particularly large this will save some disk space as well.

这篇关于除第一天外,如何使用Winston每天轮换日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆