Prevent Rails from writing development/test log files

Prevent Rails from writing development/test log files

I can’t remember the last time I needed to open/read log/development.log or log/test.log. This is just consuming disk space unnecessarily (my test.log easily reaches 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 the logs through the STDOUT though.

Rails 4 or higher

# config/environments/development.rb

# 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

# 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

# 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

# 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, let’s see their thoughts.

Comments are closed.