3

What is the best way to rotate Ruby on Rails log files daily?

I saw posts in the past. They recommended using logrotate and enable copytruncate option to avoid restarting Rails.

However, logs are lost at small time slice between copying the file and truncating it.

My project requirement does not lose logs, so we chose cronolog with ruby's stdlib Logger. The code like the following:

config.logger = Logger.new(IO.popen("/usr/sbin/cronolog #{config.paths['log'].first}.%Y%m%d", "w"))

It's mostly works correctly, but logs are mixed when logging data size more than PIPE_BUF that because using PIPE.

So what is recommended way to rotate logs in such situation?

ma2gedev
  • 151
  • 6
  • Could you explain the `PIPE_BUF` issue more? How does the issue manifest itself? – Kelvin Mar 23 '16 at 17:35
  • I think I understand it now, you're talking about atomic writes when there are concurrent writers. When using a pipe, and the log message is too large, the OS cannot guarantee atomic writes. Are you the author of the chrono_logger gem? Does it resolve the issue? – Kelvin Mar 23 '16 at 17:44
  • Yes atomic means writing logs by concurrent writers. I'm the author of chrono_logger gem. It does not necessarily solve the issue by using chrono_logger. Because chrono_logger entrust atomic logging to a file system. Some file systems takes lock when writing any size of data. For example using with CentOS's ext4 file system, chrono_logger writes logs atomically. – ma2gedev Mar 26 '16 at 13:36

0 Answers0