Log Rotation in Node.js?

前端 未结 4 1321
野的像风
野的像风 2020-12-15 05:38

In my web analytics, I am logging the data in plain text file. I want to rotate the log on a daily basis because its logging too much data. Currently I am using bunyan to ro

4条回答
  •  天涯浪人
    2020-12-15 06:36

    There's the logrotator module for log rotation that you can use regardless of the logging mechanism.

    You can specify the format option to format the date format (or any other format for that matter)

    var logrotate = require('logrotator');
    
    // use the global rotator
    var rotator = logrotate.rotator;
    
    // or create a new instance
    // var rotator = logrotate.create();
    
    // check file rotation every 5 minutes, and rotate the file if its size exceeds 10 mb.
    // keep only 3 rotated files and compress (gzip) them.
    rotator.register('/var/log/myfile.log', {
      schedule: '5m', 
      size: '10m', 
      compress: true, 
      count: 3, 
      format: function(index) {
        var d = new Date();
        return d.getDate()+"-"+d.getMonth()+"-"+d.getFullYear();
      }
    });
    

提交回复
热议问题