From 56109a3a449162ac6d7544d9143ad8cbfc21bf45 Mon Sep 17 00:00:00 2001 From: Matthew Bernhardt Date: Wed, 9 Sep 2026 15:25:05 -0400 Subject: [PATCH 1/4] First attempt at semantic logging This defines different appenders for dev and prod tiers. The prod tier is taken from the gem docs, the dev tier is a bit of a judgement call on my part. --- Gemfile | 4 ++++ Gemfile.lock | 9 +++++++++ config/application.rb | 5 +++++ config/environments/development.rb | 8 ++++++++ config/environments/production.rb | 5 +++++ 5 files changed, 31 insertions(+) diff --git a/Gemfile b/Gemfile index 8a6513fd..e3e76219 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,10 @@ gem 'tzinfo-data', platforms: %i[windows jruby] gem 'rack-cors' +# Replace default logger with semantic logging +gem 'rails_semantic_logger' +gem 'amazing_print' + # Use Redis adapter to run Action Cable in production # gem "redis", ">= 4.0.1" diff --git a/Gemfile.lock b/Gemfile.lock index 3067fd64..b457e348 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -92,6 +92,7 @@ GEM activerecord (>= 6.0, < 9.0) kaminari (~> 1.2.2) aes_key_wrap (1.1.0) + amazing_print (2.0.0) annotaterb (4.24.0) activerecord (>= 6.0.0) activesupport (>= 6.0.0) @@ -353,6 +354,10 @@ GEM rails-html-sanitizer (1.7.1) loofah (~> 2.25, >= 2.25.2) nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) + rails_semantic_logger (5.2.0) + rack + railties (>= 7.2) + semantic_logger (>= 5.1) railties (8.1.3.1) actionpack (= 8.1.3.1) activesupport (= 8.1.3.1) @@ -434,6 +439,8 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 4.0) websocket (~> 1.0) + semantic_logger (5.1.0) + concurrent-ruby (~> 1.0) sentry-rails (6.7.0) railties (>= 5.2.0) sentry-ruby (~> 6.7.0) @@ -524,6 +531,7 @@ PLATFORMS DEPENDENCIES administrate (~> 1.0.0) + amazing_print annotaterb awesome_print barnes @@ -552,6 +560,7 @@ DEPENDENCIES puma (>= 5.0) rack-cors rails (~> 8.1.0) + rails_semantic_logger rubocop rubocop-capybara rubocop-graphql diff --git a/config/application.rb b/config/application.rb index 8428ab02..b66272c5 100644 --- a/config/application.rb +++ b/config/application.rb @@ -23,5 +23,10 @@ class Application < Rails::Application # # config.time_zone = "Central Time (US & Canada)" # config.eager_load_paths << Rails.root.join("extras") + + # This application leverages SemanticLogger for more useful logging and + # insights. The details of that implementation vary by environment, but + # certain configuration is the same everywhere. + SemanticLogger.application = ENV.fetch("RAILS_APP_NAME", "tacos") end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 09ebac9a..1e5f34ca 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -40,6 +40,14 @@ # Set localhost to be used by links generated in mailer templates. config.action_mailer.default_url_options = { host: "localhost", port: 3000 } + # Log configuration for rails_semantic_logger + config.rails_semantic_logger.appenders do |appenders| + appenders.add(file_name: "log/#{Rails.env}.log", formatter: :color) + appenders.add_server( + formatter: {color: {ap: {multiline: true}}} + ) + end + # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log diff --git a/config/environments/production.rb b/config/environments/production.rb index b84de475..3f345d7a 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -39,6 +39,11 @@ # Skip http-to-https redirect for the default health check endpoint. # config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } } + # Log configuration for rails_semantic_logger + config.rails_semantic_logger.appenders do |appenders| + appenders.add(io: $stdout, formatter: :json) + end + # Log to STDOUT by default config.logger = ActiveSupport::Logger.new(STDOUT) .tap { |logger| logger.formatter = ::Logger::Formatter.new } From 9ac4339347a233b5d6b966e21b21ffd2e82159f6 Mon Sep 17 00:00:00 2001 From: Matthew Bernhardt Date: Wed, 9 Sep 2026 16:18:17 -0400 Subject: [PATCH 2/4] Respond to code review feedback --- Gemfile | 4 +++- config/environments/development.rb | 3 +-- config/environments/production.rb | 5 ----- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Gemfile b/Gemfile index e3e76219..84373293 100644 --- a/Gemfile +++ b/Gemfile @@ -72,7 +72,6 @@ gem 'rack-cors' # Replace default logger with semantic logging gem 'rails_semantic_logger' -gem 'amazing_print' # Use Redis adapter to run Action Cable in production # gem "redis", ">= 4.0.1" @@ -112,6 +111,9 @@ group :development, :test do end group :development do + # We apply robust formatting to log messages in development + gem 'amazing_print' + # Add annotations to model, test, fixtures when run gem 'annotaterb' diff --git a/config/environments/development.rb b/config/environments/development.rb index 1e5f34ca..d14ba51e 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -78,8 +78,7 @@ # Raise error when a before_action's only/except options reference missing actions. config.action_controller.raise_on_missing_callback_actions = true - # Local logging overrides - config.logger = Logger.new(STDOUT) + # Local logging level config.log_level = :info # Apply autocorrection by RuboCop to files generated by `bin/rails generate`. diff --git a/config/environments/production.rb b/config/environments/production.rb index 3f345d7a..0ee4a1e9 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -44,11 +44,6 @@ appenders.add(io: $stdout, formatter: :json) end - # Log to STDOUT by default - config.logger = ActiveSupport::Logger.new(STDOUT) - .tap { |logger| logger.formatter = ::Logger::Formatter.new } - .then { |logger| ActiveSupport::TaggedLogging.new(logger) } - # Log to STDOUT with the current request id as a default log tag. config.log_tags = [ :request_id ] From ce691057d32785a54411ba122fd3639fa801a122 Mon Sep 17 00:00:00 2001 From: Matthew Bernhardt Date: Thu, 10 Sep 2026 10:35:05 -0400 Subject: [PATCH 3/4] Code review feedback - Remove logging to a file in dev environments - Move the application name to the prod environment specifically --- config/application.rb | 5 ----- config/environments/development.rb | 1 - config/environments/production.rb | 1 + 3 files changed, 1 insertion(+), 6 deletions(-) diff --git a/config/application.rb b/config/application.rb index b66272c5..8428ab02 100644 --- a/config/application.rb +++ b/config/application.rb @@ -23,10 +23,5 @@ class Application < Rails::Application # # config.time_zone = "Central Time (US & Canada)" # config.eager_load_paths << Rails.root.join("extras") - - # This application leverages SemanticLogger for more useful logging and - # insights. The details of that implementation vary by environment, but - # certain configuration is the same everywhere. - SemanticLogger.application = ENV.fetch("RAILS_APP_NAME", "tacos") end end diff --git a/config/environments/development.rb b/config/environments/development.rb index d14ba51e..9e103020 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -42,7 +42,6 @@ # Log configuration for rails_semantic_logger config.rails_semantic_logger.appenders do |appenders| - appenders.add(file_name: "log/#{Rails.env}.log", formatter: :color) appenders.add_server( formatter: {color: {ap: {multiline: true}}} ) diff --git a/config/environments/production.rb b/config/environments/production.rb index 0ee4a1e9..6efc11ad 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -40,6 +40,7 @@ # config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } } # Log configuration for rails_semantic_logger + SemanticLogger.application = ENV.fetch("RAILS_APP_NAME", "tacos") config.rails_semantic_logger.appenders do |appenders| appenders.add(io: $stdout, formatter: :json) end From d21bce91c65e03fc7852637f60cad59ee58f0e18 Mon Sep 17 00:00:00 2001 From: Matthew Bernhardt Date: Thu, 10 Sep 2026 17:01:10 -0400 Subject: [PATCH 4/4] Make multiline logging configurable --- config/environments/development.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/environments/development.rb b/config/environments/development.rb index 9e103020..51f66ee8 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -42,8 +42,9 @@ # Log configuration for rails_semantic_logger config.rails_semantic_logger.appenders do |appenders| + multiline = ENV.fetch("SEMANTIC_LOGGER_MULTILINE", "true").downcase == "true" appenders.add_server( - formatter: {color: {ap: {multiline: true}}} + formatter: {color: {ap: {multiline: multiline}}} ) end