Winston: Add ability for File transport to split up logs by date

Created on 29 May 2011  ·  12Comments  ·  Source: winstonjs/winston

@indexzero -

How would you feel about adding an optional API parameter to the File transport so that it can split up log files by date? The idea would be that on each new day, a new log file would be created and Winston would automatically handle all of this / know which log file to write to based on the date.

feature request

Most helpful comment

There is no need to complicate the winston code when there is a standard tool for this task: Logrotate (http://linuxcommand.org/man_pages/logrotate8.html)

All 12 comments

Would accept patch request for this

Cool! Any suggestions for an API? I was thinking something like:

           // Specify path option and default to spanning by day ( 24 hours in each file )
           new (winston.transports.File)({ path: './logs' })

but there might be some utility in allowing for a custom date / time filter.

There is no need to complicate the winston code when there is a standard tool for this task: Logrotate (http://linuxcommand.org/man_pages/logrotate8.html)

@indexzero Is patch for this still acceptable ? I know this can by implemented via filename option but I can add another option for this.

it's not the task of individual daemons to each implement log rotation. this is why we have tools like logrotate, like @cemuzunlar said.

@cemuzunlar @Dieterbe

I agree, ideally, this wouldn't be winston's problem, but

  • winston does run on Windows, which does not have logrotate, despite several (feeble looking) simple attempts on various blogs (and some suggestions to use cygwin + a self-compiled logrotate, but I doubt that would even be possible on theoretical grounds, assuming that winston keeps the file open for writing, which severely restricts what you can do with that file on Windows)
  • winston's file transport already does log rotation, only it splits by file size, not date. And it also deletes old files, so it's all of basic log rotation functionality

I see...
thinking out loud: moving log rotation logic into a separate node program, so it's a reusable simple logrotate clone that works across platforms.

++1

While I am also an advocate of logrotate, it does not do its work alone. Logrotate is so very usable because it provides a method to tell programs that it is time to flush and close their logs.

The file transport has a _createStream.checkFile.createAndFlush function that at first glance looks like it should do the appropriate things. To use this for logrotate, have your node program trap SIGHUP and access this function. I don't have time before my dayjob to post code, but I will try tonight and post my results.

Responding to SIGHUP in this fashion should be transportable to windows, and then whatever log rotation you use on windows (node library or other magic) is left as an exercise to the reader.

In case anyone else is looking for this:

Thanks for the tip, @pccowboy. I'm a big advocate of logrotate too. I'm reluctant to use one configuration for node.js service (ie winston File maxsize, etc) and another (logrotate) for other services (nginx, redis, etc) and would prefer to use logrotate for all my logfiles. I've put together a simple function which will listen to HUP signals and reopen the log file postrotate.

If I get a chance, I'll try rewrite this in a cleaner manner (ie, as a configuration option for the File transport) and create a pull request.

https://gist.github.com/suprememoocow/5133080

Fixed by #205.

awesome!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mohanen picture mohanen  ·  4Comments

xungwoo picture xungwoo  ·  3Comments

JaehyunLee-B2LiNK picture JaehyunLee-B2LiNK  ·  3Comments

greenhat616 picture greenhat616  ·  3Comments

KingRial picture KingRial  ·  3Comments