Ruby on rails log file size too large
Asked Answered
S

10

35

I stumbled to learn that my rails3.1 log file is super large, around 21mb. Is this, in terms of size normal? What the log file would like in the production environment? Besides, can I get rid of the log?thanks

Succession answered 16/10, 2011 at 11:51 Comment(0)
N
29

you can just delete the file!
Rails will create a new log if one doesn't exist.
Obviously save / backup the file if it's important, but usually it's not.
You can also zip the backuped up file (and then delete the source) if you want to keep it on the same drive but still save space.

To automatically rotate log files (the best long-term solution) use log rotate as described here:

Ruby on Rails production log rotation

then you can set it and forget it!

To actually change what gets logged see:

http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/

Nurse answered 16/10, 2011 at 11:58 Comment(3)
I mean that if it's possible that rails don't generate a log file.Succession
I don't know if that can easily be done. Maybe alias it to dev/null? Nah, better to use rotate and so I added that in too.Nurse
Updated expired link to be a real one.Nurse
R
81

The log folder of your Rails application holds three log files corresponding to each of the standard environments. Log files can grow very large over time. A rake task is provided to allow the easy clearing of the log files.

rake log:clear
# Truncates all *.log files in log/ to zero bytes 
# Specify which logs with LOGS=test,development,production
Ranit answered 17/3, 2013 at 16:46 Comment(1)
This anwser should have been accepted. On passenger deployment, once you delete log file, rails doesn't create new one.Cadman
N
29

you can just delete the file!
Rails will create a new log if one doesn't exist.
Obviously save / backup the file if it's important, but usually it's not.
You can also zip the backuped up file (and then delete the source) if you want to keep it on the same drive but still save space.

To automatically rotate log files (the best long-term solution) use log rotate as described here:

Ruby on Rails production log rotation

then you can set it and forget it!

To actually change what gets logged see:

http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/

Nurse answered 16/10, 2011 at 11:58 Comment(3)
I mean that if it's possible that rails don't generate a log file.Succession
I don't know if that can easily be done. Maybe alias it to dev/null? Nah, better to use rotate and so I added that in too.Nurse
Updated expired link to be a real one.Nurse
D
13

According to the documentation, if you want to limit the size of the log folder, put this in your 'development.rb'-file:

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.

Darkling answered 28/5, 2016 at 13:35 Comment(1)
parameters are name, shift_age and shift_size, ruby-doc.org/stdlib-1.9.3/libdoc/logger/rdoc/…Faceharden
B
6

I automatically clear the logs in development on each server start with config/initializers/clear_development_log.rb:

if Rails.env.development?
  `rake log:clear`
end
Boarfish answered 21/12, 2016 at 15:7 Comment(1)
You should add LOGS=development after log:clear so it only clears the development.log.Liquidize
I
5

You may want to use logrotate. Have a look at the answer to this question: Ruby on Rails production log rotation.

Instead answered 16/10, 2011 at 12:2 Comment(0)
G
3

Yes, You can using syntax like this:

config.logger = ActiveSupport::Logger.new(config.log_file, num_of_file_to_keep, num_of_MB*1024*1024)

Example:

config.logger = ActiveSupport::Logger.new(config.log_file, 2, 20*1024*1024)

It not only using for Rails log, you can using log file of any services run with rails, such as: rpush log, ...

Grandmotherly answered 3/8, 2016 at 6:55 Comment(1)
log_file method does not exist, at least in Rails 5+Amatol
S
1

If you don't like waiting for rake log:clear to load its environment and only want to clear one log on the fly, you can do the following:

cat /dev/null > log/mylog.log # Or whatever your log's name is

(This allows the log to stay online while the app is running, whereas rm log/mylog.log would require restarting the app.)

Syphon answered 25/9, 2020 at 19:47 Comment(0)
K
0

config.logger = ActiveSupport::Logger.new(nil) does the trick and completely disables logging to a file (console output is preserved).

Kaminsky answered 27/8, 2016 at 13:42 Comment(0)
T
0

A fair compromise, in an initializer:

Rake::Task['log:clear'].invoke if Rails.env.development? || Rails.env.test?
Triadelphous answered 31/3, 2017 at 17:26 Comment(0)
T
0

It is now possible to configure the log_file_size in the latest version of Rails. This information may be useful for you.

REF: https://guides.rubyonrails.org/configuring.html#config-log-file-size

Termor answered 22/3 at 8:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.