What is the best way to enable log rotation on a Ruby on Rails production app?
Is it by using logrotate on the hosting server or is there a set of options to use when initializing logger from the app?
What is the best way to enable log rotation on a Ruby on Rails production app?
Is it by using logrotate on the hosting server or is there a set of options to use when initializing logger from the app?
You can configure rails, to use the systems log tools.
An example in config/environments/production.rb.
# Use a different logger for distributed setups
config.logger = SyslogLogger.new
That way, you log to syslog, and can use default logrotate tools to rotate the logs.
Another option is to simply configure logrotate to pick up the logs left by rails.
On Ubuntu and Debian that would be, for example, in a file called /etc/logrotate.d/rails_example_com
.
/path/to/rails.example.com/tmp/log/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
copytruncate
}
As per suggestions below, in Rails it is advised to use copytruncate
, to avoid having to restart the Rails app.
Edit: removed "sharedscripts/endscript" since they are not used here and cause problems according to comment. And removed create 640 root adm
as per comment suggested.
logrotate
solution, it's worth @amit-saxena's answer -- suggests use of copytruncate
over the create
directive. –
Despatch endscript
without a prescript
or postscript
–
Ormazd sharedscripts
requires an endscript
. But both are not really needed and can, indeed, be removed. But only if both are removed. –
Hylozoism sharedscripts
does not require anything, check the man. This is the error: error: redis:9 unknown option 'endscript' -- ignoring line –
Ormazd copytruncate
, create
has no effect, so you should probably remove it from your example –
Inellineloquent su your_rails_user your_rails_group
with the owner and group of your log files (i.e., those of the Rails/Passenger process) or (recent versions of?) logrotate may complain about permissions. –
Vulnerary copytruncate
you probably want to also use nocreate
depending on what your main logrotate settings are, as in, do they have create
present. Look at /etc/logrotate.conf
–
Skinner sudo logrotate -f /etc/logrotate.d/rails_example_com
. This way I found out I had to add su rails_user rails_user_group
like oseiskar suggested. –
Kyoko If you are using logrotate then you can choose either of the options shown below by placing a conf file in the /etc/logrotate.d/ directory.
# Rotate Rails application logs based on file size
# Rotate log if file greater than 20 MB
/path/to/your/rails/applicaton/log/*.log {
size=20M
missingok
rotate 52
compress
delaycompress
notifempty
copytruncate
}
Or
# Rotate Rails application logs weekly
/path/to/your/rails/applicaton/log/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
copytruncate
}
Please note that copytruncate makes a backup copy of the current log and then clears the log file for continued writing. The alternative is to use create which will perform the rotation by renaming the current file and then creating a new log file with the same name as the old file. I strongly recommend that you use copytruncate unless you know that you need create. The reason why is that Rails may still keep pointing to the old log file even though its name has changed and they may require restarting to locate the new log file. copytruncate avoids this by keeping the same file as the active file.
logrotate --force $CONFIG_FILE
by specifying the config file location to run it manually. –
Spae For Rails 5, this is what I had to do to limit log size and don't change server output in the console:
According to the documentation, if you want to limit the size of the log folder, put this in your environment-file ('development.rb'/'production.rb').
config.logger = ActiveSupport::Logger.new(config.paths['log'].first, 1, 50 * 1024 * 1024)
With this, your log files will never grow bigger than 50Mb. You can change the size to your own preference. The ‘1’ in the second parameter means that 1 historic log file will be kept, so you’ll have up to 100Mb of logs – the current log and the previous chunk of 50Mb.
config.paths['log'].first
I'd put Rails.root.join('log', "#{Rails.env}.log")
–
Lastly config.logger = ActiveSupport::Logger.new(config.log_file, 1, 20*1024*1024)
–
Lalise 50.megabytes
is the same as 50 * 1024 * 1024
, but much easier to understand. See ActiveSupport core extensions for more details. –
Castilian Rails.application.config.paths['log'].first
return exactly this file –
Selfrighteous For Rails 5, if you want daily log rotation, you only need this:
config.logger = ActiveSupport::Logger.new(config.paths['log'].first, shift_age = 'daily')
According the documentation, you can use daily
, weekly
or monthly
.
For every log: Rails log, Rpush log, ... You can use like this in your config file of service:
config.log_file = 'log/rpush.log'
config.logger = ActiveSupport::Logger.new(config.log_file, 1, 20.megabytes)
It means: only save 1 previous log file after split. Main log size never over 20 MB.
Enable to send logs to the loggly using rails logglier as following in my environments/production.rb file. rails version is 4.1.0
RailsApplication::Application.configure do
require 'logglier'
config.logger = Logglier.new(<https://logs-01.loggly.com/inputs/inputkey>)
log.info("hello from logglier")
end
© 2022 - 2024 — McMap. All rights reserved.