Why does rails crash when rotating logs? What is the proper way to rotate logs in rails?

StackOverflow https://stackoverflow.com/questions/9131666

  •  22-04-2021
  •  | 
  •  

Question

I did not want to go through the headache of setting up 4 node-servers with 3 applications each by using logrotate for UNIX.

So I decided to setup my rails applications with the following inside environment.rb:

  log_path = "#{Rails.root}/log/#{ENV['RAILS_ENV']}.log"
  shift_age = 'weekly' # you can either 1) specify 2 params 50, 1024bytes OR 2) specify 1 param 'daily | weekly | monthly'
  config.logger = Logger.new(log_path, shift_age)

However, this caused the rails application to break once the files were rolled over.

I am wondering, what is the best way to rotate logs with rails applications? Is it logrotate? Thanks.

Was it helpful?

Solution

This happens because one process is trying to rotate the log file at the same time that the another process is trying to rotate it, a race condition occurs, and the server stops responding to requests, as a Fatal Exception is raise

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top