除第一天之外,如何使用Winston每日轮换日志

时间:2023-02-05 22:03:01

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

我需要每天轮换日志,除了当天的文件。我正在使用winston和winston-daily-rotate-files库。

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

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

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.

但是,我真的需要生成文件“info.log”,在这一天之后,应该重命名为“info.log.2016-08-09”,并为当天创建一个新的“info.log”。我明白这是其他应用程序中的正常行为。

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"
    })
   ]
});
 

1 个解决方案

#1


2  

Well one workaround can be to have one more transport to info.log

一个解决方法可以是再向info.log传输一个

Just like this :

像这样 :

var logger = new (winston.Logger)({
  transports: [
    new dailyRotateFile(  
      {
        //your definition of rotate file
      }),
    new (winston.transports.File)({ filename: 'info.log' })
   ]
});

And then set up some cron to delete info.log at midnight, i.e. node-schedule

然后设置一些cron来在午夜删除info.log,即node-schedule

However, with this approach there can be little inconsistency, if something is run through midnight, it can log few lines into info.log which belongs to next day and then it is deleted, therefore info.log can be incompleted.

然而,使用这种方法可能几乎没有不一致,如果某些事情在午夜运行,它可以将几行记录到属于第二天的info.log中,然后将其删除,因此info.log可以不完整。

But all the logs with this info.log.2016-08-09 format remains full and unaffected.

但是这个info.log.2016-08-09格式的所有日志都保持完整且不受影响。

So it is to consider if very small chance to have incomplete info.log for one day is acceptable. (however you can create more advanced checker that does not just delete file, but looks if exist file of new day and if so, it looks whats inside and then delete only the logs from previous days from info.log and it does not delete it all at once)

因此,可以考虑是否有一天获得不完整的info.log的可能性非常小。 (但是你可以创建更高级的检查器,不只是删除文件,但查看是否存在新的一天的文件,如果是,它看起来里面是什么,然后从info.log只删除前几天的日志,它不会删除它一次全部)

#1


2  

Well one workaround can be to have one more transport to info.log

一个解决方法可以是再向info.log传输一个

Just like this :

像这样 :

var logger = new (winston.Logger)({
  transports: [
    new dailyRotateFile(  
      {
        //your definition of rotate file
      }),
    new (winston.transports.File)({ filename: 'info.log' })
   ]
});

And then set up some cron to delete info.log at midnight, i.e. node-schedule

然后设置一些cron来在午夜删除info.log,即node-schedule

However, with this approach there can be little inconsistency, if something is run through midnight, it can log few lines into info.log which belongs to next day and then it is deleted, therefore info.log can be incompleted.

然而,使用这种方法可能几乎没有不一致,如果某些事情在午夜运行,它可以将几行记录到属于第二天的info.log中,然后将其删除,因此info.log可以不完整。

But all the logs with this info.log.2016-08-09 format remains full and unaffected.

但是这个info.log.2016-08-09格式的所有日志都保持完整且不受影响。

So it is to consider if very small chance to have incomplete info.log for one day is acceptable. (however you can create more advanced checker that does not just delete file, but looks if exist file of new day and if so, it looks whats inside and then delete only the logs from previous days from info.log and it does not delete it all at once)

因此,可以考虑是否有一天获得不完整的info.log的可能性非常小。 (但是你可以创建更高级的检查器,不只是删除文件,但查看是否存在新的一天的文件,如果是,它看起来里面是什么,然后从info.log只删除前几天的日志,它不会删除它一次全部)