From e0a94f6a6e5955cdb895d3cad2adfff1d18ad5ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Mon, 23 May 2022 16:20:27 +0200 Subject: [PATCH] Reduce log size in development/test environments Code based on the logger Rails uses by default; as mentioned in the Rails configuration guide: > [the logger] defaults to an instance of ActiveSupport::TaggedLogging > that wraps an instance of ActiveSupport::Logger which outputs a log to > the log/ directory. You can supply a custom logger, to get full > compatibility you must follow these guidelines: > > * To support a formatter, you must manually assign a formatter from > the config.log_formatter value to the logger. > * To support tagged logs, the log instance must be wrapped with > ActiveSupport::TaggedLogging. > * To support silencing, the logger must include > ActiveSupport::LoggerSilence module. The ActiveSupport::Logger class > already includes these modules. Just like the documentation mentions, we're enabling log rotation using "1" as the number of old files to keep and then the maximum size of the log file. --- config/environments/development.rb | 6 ++++++ config/environments/test.rb | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/config/environments/development.rb b/config/environments/development.rb index 1ad4459f3..d3adf353f 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -64,6 +64,12 @@ Rails.application.configure do config.action_mailer.preview_path = "#{Rails.root}/spec/mailers/previews" + # Limit size of local logs + # TODO: replace with config.log_file_size after upgrading to Rails 7.1 + logger = ActiveSupport::Logger.new(config.default_log_file, 1, 100.megabytes) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + config.after_initialize do Bullet.enable = true Bullet.bullet_logger = true diff --git a/config/environments/test.rb b/config/environments/test.rb index 09ee31b58..64c8b7519 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -54,6 +54,12 @@ Rails.application.configure do # Raises error for missing translations. # config.action_view.raise_on_missing_translations = true + # Limit size of local logs + # TODO: replace with config.log_file_size after upgrading to Rails 7.1 + logger = ActiveSupport::Logger.new(config.default_log_file, 1, 100.megabytes) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + config.after_initialize do Bullet.enable = true Bullet.bullet_logger = true