trentm / node-bunyan

a simple and fast JSON logging module for node.js services
Other
7.17k stars 516 forks source link

Variant on rotating-file stream #179

Open nikhilk opened 9 years ago

nikhilk commented 9 years ago

I was looking for the following capability ... it doesn't seem possible with rotating-file (is it?).

I'd like to have log files generated with the time-period they correspond to in the log file name. For example with daily rotation, the log files would be foo.2014-10-21.log, foo.2014-10-22.log and so on, and for hourly they may be foo.2014-10-21-01.log through foo.2014-10-21-24.log (on a given day).

Effectively the time-period is encoded in the log file name, so its easy to pick a log file from a set of collected log files.

Alternatively, it would be nice if there was an event one could implement when log files are being rotated to provide a name that the current file should be renamed to, and let the application implement its own naming scheme.

trentm commented 9 years ago

Correct that the current bunyan 'rotating-file' cannot do this. From the readme:

*Currently*, there is no support for providing a template for the rotated
files, or for rotating when the log reaches a threshold size.

Frankly my best advice is to use your system's native log file rotation facility, e.g. logrotate on Linux, logadm on Illumos/SmartOS. However, I'd definitely not oppose a PR to add filename formatting support with a template. My preference would be for the syntax for the output filename template to copy logadm and/or logrotate similar syntax within reason.

Adding an event for rotation is a great idea. I don't recall right now if that would be easy (i.e. if the rotating-file "stream" has a reference to its Logger on which the event should be emitted.

nikhilk commented 9 years ago

I might not be able to take on the work immediately, but I'll certainly do what I can to try to get to it sometime not too far off.

One hopefully quick question for you - my thought with the event-based scheme, where the app can provide a log file name is it also helps simplify the rotation logic ... i.e it doesn't have to delete the oldest, and rename all others to make room for the latest. It would simply name the file with the desired name to begin with. I noticed an error with multiple loggers writing to the same file - the 1st one would do a move, and other loggers would fail in their rotation logic, since the file had already been moved. Naming the file without requiring moves would have a nice side-benefit of avoiding this issue should multiple loggers be associated with a stream bound to the same file. Any thoughts/confirmation?

Rcomian commented 8 years ago

I've just release v1.5.0 of my rotating file stream which adds templating based on timestamps.

If you want this feature, please check it out: bunyan-rotating-file-stream

tonylukasavage commented 7 years ago

Checking out @Rcomian version now, as it fits my needs, but would love to see the templating incorporated into bunyan itself. @trentm I read the TODO notes in the source... would you be open to adding strftime as a dependency to bunyan? If yes, I'd be happy to take a crack at a PR to get it to support templates for rotating files.