The Crystal Programming Language Forum

Log Rotation

Ok, so next logger question…

Is there a built-in way (or 3rd party Shard) to have a logger alternate which file it logs to? If not, I have some ideas of how I might implement this, but figured I’d ask around first…

One scenario I’ve seen is that you start with something like app.1.log, then goes with app.2.log, and continues cycling up thru app.X.log, then loops back around to app.1.log.

In other words:

(a) Include a log loop counter or a timestamp in the filename (as a prefix, postfix, or midfix), e.g.:

  • like: my_log.1.log and my_log.2.log, or 01.my.log, or my.log.001, etc

(b) Alternate through X log files

(c) When some condition is met (e.g.: log file size or line count or etc), then it will

  • For counter-named log files, it will cycle back to the first log file and replaces that older file with the new log contents; and proceed likewise when it is ready to go to the next log file in the cycle.
  • For timestamp-named log files, it will start a new log file, optionally erasing old log files (older than X files or some relative timestamp)

I think this would be better served by a dedicated log rotating tool.

1 Like

I’ve extracted this second topic from Log to multiple files - #3 by drhuffman12

It’s easier if discussions stay focused on a single topic.

1 Like

just for reference and because I use it constantly: logrotate(8) - Linux man page

It’s avaliable by default on all linux distributions I know, and I assume other Operating systems must have some equivalent function available?

1 Like

Thanks @mavu ; logrotate seems to have a nice assortment of features!