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 bunyan
package 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?
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.
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:
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:
where my logger file is:
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)