Log Rotation in Node.js?

為{幸葍}努か 提交于 2019-11-27 14:38:41

问题


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 rotate the logs.

Problem I am facing

It is rotating the file correctly, but rotated log file are in the name log.0, log.1, etc. I want the file name to be log.05-08-2013, log.04-08-2013

I can't edit the source of the bunyanpackage because we are installing the modules using package.json via NPM.

So my question is - Is there any other log rotation in Node.js that meets my requirement?


回答1:


Winston does support log rotation using a date in the file name. Take a look at this pull request which adds the feature and was merged four months ago. Unfortunately the documentation isn't listed on the site, but there is another pull request pending to fix that. Based on that documentation, and the tests for the log rotation features, you should be able to just add it as a new Transport to enable the log rotation functionality. Something like the following:

winston.add(winston.transports.DailyRotateFile, {
  filename: './logs/my.log',
  datePattern: '.dd-MM-yyyy'
});



回答2:


If you also want to add logrotate (e.g. remove logs that are older than a week) in addition to saving logs by date, you can add the following code:

var fs = require('fs');
var path = require("path");
var CronJob = require('cron').CronJob;
var _ = require("lodash");
var logger = require("./logger");

var job = new CronJob('00 00 00 * *', function(){
    // Runs every day
    // at 00:00:00 AM.
    fs.readdir(path.join("/var", "log", "ironbeast"), function(err, files){
        if(err){
            logger.error("error reading log files");
        } else{
            var currentTime = new Date();
            var weekFromNow = currentTime -
                (new Date().getTime() - (7 * 24 * 60 * 60 * 1000));
            _(files).forEach(function(file){
                var fileDate = file.split(".")[2]; // get the date from the file name
                if(fileDate){
                    fileDate = fileDate.replace(/-/g,"/");
                    var fileTime = new Date(fileDate);
                    if((currentTime - fileTime) > weekFromNow){
                        console.log("delete fIle",file);
                        fs.unlink(path.join("/var", "log", "ironbeast", file),
                            function (err) {
                                if (err) {
                                    logger.error(err);
                                }
                                logger.info("deleted log file: " + file);
                            });
                    }
                }
            });
        }
    });
}, function () {
    // This function is executed when the job stops
    console.log("finished logrotate");
},
true, /* Start the job right now */
'Asia/Jerusalem' /* Time zone of this job. */
);

where my logger file is:

var path = require("path");
var winston = require('winston');

var logger = new winston.Logger({
transports: [
    new winston.transports.DailyRotateFile({
        name: 'file#info',
        level: 'info',
        filename: path.join("/var", "log", "MY-APP-LOGS", "main.log"),
        datePattern: '.MM--dd-yyyy'
    }),
    new winston.transports.DailyRotateFile({
        name: 'file#error',
        level: 'error',
        filename: path.join("/var", "log", "MY-APP-LOGS", "error.log"),
        datePattern: '.MM--dd-yyyy',
        handleExceptions: true
    })
]});

module.exports = logger;



回答3:


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();
  }
});



回答4:


mongodb

winston itself does not support log rotation. My bad.

mongodb has a log rotation use case. Then you can export the logs to file names per your requirement.

winston also has a mongodb transport but I don't think it supports log rotation out of the box judging from its API.

This may be an overkill though.

forking bunyan

You can fork bunyan and add your repo's url in package.json.

This is the easiest solution if you're fine with freezing bunyan's feature or maintaining your own code.

As it is an open source project, you can even add your feature to it and submit a pull request to help improve bunyan.



来源:https://stackoverflow.com/questions/18055971/log-rotation-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!