From e077c7e8907d773e8e4f78a9c1c878df36487d79 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javi=20Mart=C3=ADn?= Date: Wed, 28 Jun 2023 14:50:47 +0200 Subject: [PATCH] Rotate log files on production and staging The 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. --- config/environments/production.rb | 5 +++++ config/environments/staging.rb | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/config/environments/production.rb b/config/environments/production.rb index 47c8ac22a..450bbbeb8 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -87,6 +87,11 @@ Rails.application.configure do # Use default logging formatter so that PID and timestamp are not suppressed. config.log_formatter = ::Logger::Formatter.new + # Rotate logger + logger = ActiveSupport::Logger.new(config.default_log_file, "daily") + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + # Use a different logger for distributed setups. # require "syslog/logger" # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 87c57f36d..f483267db 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -86,6 +86,11 @@ Rails.application.configure do # Use default logging formatter so that PID and timestamp are not suppressed. config.log_formatter = ::Logger::Formatter.new + # Rotate logger + logger = ActiveSupport::Logger.new(config.default_log_file, "daily") + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + # Use a different logger for distributed setups. # require "syslog/logger" # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name")