34

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

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
Jason
  • 371
  • 1
  • 4
  • 7

9 Answers9

80

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
Sheharyar
  • 73,588
  • 21
  • 168
  • 215
Raj Adroit
  • 3,828
  • 5
  • 31
  • 44
  • 5
    This anwser should have been accepted. On passenger deployment, once you delete log file, rails doesn't create new one. – EGurelli Nov 19 '15 at 17:14
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/

Community
  • 1
  • 1
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
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.

Fellow Stranger
  • 32,129
  • 35
  • 168
  • 232
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
Drew Stephens
  • 17,207
  • 15
  • 66
  • 82
5

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

Community
  • 1
  • 1
htanata
  • 36,666
  • 8
  • 50
  • 57
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, ...

ThienSuBS
  • 1,574
  • 18
  • 26
0

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

Artur INTECH
  • 6,024
  • 2
  • 37
  • 34
0

A fair compromise, in an initializer:

Rake::Task['log:clear'].invoke if Rails.env.development? || Rails.env.test?
Dorian
  • 22,759
  • 8
  • 120
  • 116
0

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.)

JellicleCat
  • 28,480
  • 24
  • 109
  • 162