Ir para conteúdo principal
Lucas Caton

Prevent Rails from writing development/test log files

Lucas Caton

Lucas Caton

@lucascaton
I can't remember the last time I needed to open/read log/development.log or log/test.log. These files usualy just consume disk space unnecessarily (some test.log files can easily reach more than 1 GB).
After talking to some other developers, all of them agreed they don't use it as well. So what I've been doing in my projects is adding the following code (note that it'll still display logs through the STDOUT though):

Rails 4+

config/environments/development.rb:
ruby
# Prevents from writing logs on `log/development.log`
logger           = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger    = ActiveSupport::TaggedLogging.new(logger)
config/environments/test.rb:
ruby
# Prevents from writing logs on `log/test.log`
config.log_level = :warn
logger           = ActiveSupport::Logger.new(STDOUT)
logger.formatter = config.log_formatter
config.logger    = ActiveSupport::TaggedLogging.new(logger)

Rails 3

config/environments/development.rb:
ruby
# Prevents from writing logs on `log/development.log`
logger        = ::Logger.new(STDOUT)
config.logger = ActiveSupport::TaggedLogging.new(logger)

# Replace `config.active_support.deprecation = :log` with:
config.active_support.deprecation = :stderr
config/environments/test.rb:
ruby
# Prevents from writing logs on `log/test.log`
config.log_level = :warn
logger           = ::Logger.new(STDOUT)
config.logger    = ActiveSupport::TaggedLogging.new(logger)
Ps.: I've sent a suggestion to rubyonrails-core mailing list to make it default from next Rails versions, let's see their thoughts.

Post atualizado em 02/10/2017, 10:00:00