Log Rotation in Node.js?

karthick picture karthick · Aug 5, 2013 · Viewed 23.9k times · Source

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?

Answer

Timothy Strimple picture Timothy Strimple · Aug 8, 2013

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