From bc18fc66ac0e6687a6bd53fc073b98b363dd112a Mon Sep 17 00:00:00 2001 From: Isra Jazairi Date: Thu, 1 Oct 2026 14:30:34 -0700 Subject: [PATCH 1/3] Switch from lograge to rails-semantic-logger Why these changes are being introduced: This is part of a broader attempt to improve logging. Relevant ticket(s): - [ETD-708](https://mitlibraries.atlassian.net/browse/ETD-708) How this addresses that need: This configures `rails-semantic-logger` to log to STDOUT in JSON, using color highlights in development. Side effects of this change: None. --- Gemfile | 4 +++- Gemfile.lock | 27 +++++++++++++++++++++------ config/environments/development.rb | 14 +++++--------- config/environments/production.rb | 13 ++++++++----- config/initializers/lograge.rb | 20 -------------------- 5 files changed, 37 insertions(+), 41 deletions(-) delete mode 100644 config/initializers/lograge.rb diff --git a/Gemfile b/Gemfile index 38d9df3d..60c8e6de 100644 --- a/Gemfile +++ b/Gemfile @@ -20,7 +20,6 @@ gem 'flipflop' gem 'jquery-rails' gem 'json' gem 'kaminari' -gem 'lograge' gem 'marc' gem 'mitlibraries-theme', git: 'https://github.com/mitlibraries/mitlibraries-theme', tag: 'v1.5' gem 'net-imap', require: false @@ -31,6 +30,8 @@ gem 'omniauth-saml' gem 'paper_trail' gem 'puma' gem 'rails', '~> 8.1.0' +gem 'rails_cloudflare_turnstile' +gem 'rails_semantic_logger' gem 'rubyzip' gem 'sass-rails' gem 'sentry-rails' @@ -55,6 +56,7 @@ group :development, :test do end group :development do + gem 'amazing_print' gem 'annotaterb' gem 'dotenv-rails' gem 'letter_opener' diff --git a/Gemfile.lock b/Gemfile.lock index 7c3b737f..732818db 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -91,6 +91,7 @@ GEM actionview (>= 6.0, < 9.0) activerecord (>= 6.0, < 9.0) kaminari (~> 1.2.2) + amazing_print (3.0.0) annotaterb (4.24.0) activerecord (>= 6.0.0) activesupport (>= 6.0.0) @@ -174,6 +175,12 @@ GEM erb (6.0.7) erubi (1.13.1) execjs (2.10.1) + faraday (2.14.4) + faraday-net_http (>= 2.0, < 3.5) + json + logger + faraday-net_http (3.4.4) + net-http (~> 0.5) ffi (1.17.3) flipflop (2.8.0) activesupport (>= 4.0) @@ -222,11 +229,6 @@ GEM rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) logger (1.7.0) - lograge (0.15.0) - actionpack (>= 4) - activesupport (>= 4) - railties (>= 4) - request_store (~> 1.0) loofah (2.25.2) crass (~> 1.0.2) nokogiri (>= 1.12.0) @@ -250,6 +252,8 @@ GEM minitest (>= 5.0, < 7) ruby-progressbar msgpack (1.8.4) + net-http (0.9.1) + uri (>= 0.11.1) net-imap (0.6.6) date net-protocol @@ -326,6 +330,13 @@ GEM 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-mermaid_erd (0.8.0) rails (>= 5.2) + rails_cloudflare_turnstile (0.5.1) + faraday (>= 1.0, < 3.0) + rails (>= 6.0, < 8.2) + 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) @@ -402,6 +413,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) @@ -468,6 +481,7 @@ PLATFORMS DEPENDENCIES administrate (~> 1.0.0) + amazing_print annotaterb aws-sdk-rails aws-sdk-s3 @@ -490,7 +504,6 @@ DEPENDENCIES kaminari letter_opener listen - lograge marc minitest (< 6) minitest-reporters @@ -505,6 +518,8 @@ DEPENDENCIES puma rails (~> 8.1.0) rails-mermaid_erd + rails_cloudflare_turnstile + rails_semantic_logger rubocop rubocop-rails rubyzip diff --git a/config/environments/development.rb b/config/environments/development.rb index d9ccf12e..08feba27 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -86,16 +86,12 @@ # Raises error for missing translations. # config.i18n.raise_on_missing_translations = true - # allow configurable log levels, default to debug - config.log_level = ENV['LOG_LEVEL'] || :debug + config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "debug") - # Use default logging formatter so that PID and timestamp are not suppressed. - config.log_formatter = ::Logger::Formatter.new - - if ENV["RAILS_LOG_TO_STDOUT"].present? - logger = ActiveSupport::Logger.new(STDOUT) - logger.formatter = config.log_formatter - config.logger = ActiveSupport::TaggedLogging.new(logger) + # Configure Rails Semantic Logger + config.rails_semantic_logger.appenders do |appenders| + # appenders.add(file_name: "log/#{Rails.env}.log", formatter: :color) # uncomment to log to file + appenders.add(io: $stdout, formatter: :color) end # Annotate rendered view with file names. diff --git a/config/environments/production.rb b/config/environments/production.rb index 5640fff3..46c60116 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -36,15 +36,18 @@ # Skip http-to-https redirect for the default health check endpoint. # config.ssl_options = { redirect: { exclude: ->(request) { request.path == "/up" } } } - # Log to STDOUT by default - config.log_tags = [ :request_id ] - config.logger = ActiveSupport::Logger.new(STDOUT) - .tap { |logger| logger.formatter = ::Logger::Formatter.new } - .then { |logger| ActiveSupport::TaggedLogging.new(logger) } + # Include request ID as a structured field. + config.log_tags = { request_id: :request_id } # Change to "debug" to log everything (including potentially personally-identifiable information!). config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "info") + # Configure Rails Semantic Logger to log to STDOUT in JSON format + SemanticLogger.application = ENV.fetch("RAILS_APP_NAME", "thing") + config.rails_semantic_logger.appenders do |appenders| + appenders.add(io: $stdout, formatter: :json) + end + # Prevent health checks from clogging up the logs. config.silence_healthcheck_path = "/up" diff --git a/config/initializers/lograge.rb b/config/initializers/lograge.rb deleted file mode 100644 index 44fa85fb..00000000 --- a/config/initializers/lograge.rb +++ /dev/null @@ -1,20 +0,0 @@ -Rails.application.configure do - unless ENV["DISABLE_LOGRAGE"].present? - config.lograge.enabled = true - end - - config.lograge.custom_options = lambda do |event| - exceptions = %w(controller action format id) - { - params: event.payload[:params].except(*exceptions), - time: Time.now.utc().iso8601(3), - } - end - - # This is mostly only useful when central logging is set up - config.lograge.custom_payload do |controller| - { - host: controller.request.host - } - end -end From 1affa430691efd73fbd132eb09db41904ac4db3a Mon Sep 17 00:00:00 2001 From: Isra Jazairi Date: Thu, 1 Oct 2026 15:03:31 -0700 Subject: [PATCH 2/3] Update readme --- README.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 80811c0b..71d1d2a9 100644 --- a/README.md +++ b/README.md @@ -87,15 +87,15 @@ postgres config. ## Environment variables -`DISABLE_LOGRAGE` - set this in to disable lograge single line logging config -and use rails standard verbose logging. - `JS_EXCEPTION_LOGGER_KEY` - set this to the value of the exception monitor public post key to enable capturing javascript exceptions. -`LOG_LEVEL` - we set sane defaults in development or production, but you can +`RAILS_LOG_LEVEL` - we set sane defaults in development or production, but you can override easily with this ENV if you need to get more details. +`RAILS_APP_NAME` - overrides the application name used by semantic logger in +structured log output. + `MAINTENANCE_MODE` - this toggles a [Flipflop feature](https://github.com/voormedia/flipflop) that disables thesis transfers and informs transfer submitters that the application is under maintenance. `MAINTENANCE_MESSAGE_TRANSFER` - provides a custom message to transfer submitters if more details are necessary. @@ -107,9 +107,6 @@ requests that come to the app will redirect to the root of this domain. This is useful to prevent access to herokuapp.com domains as well as any legacy domains you'd like to handle. -`RAILS_LOG_TO_STDOUT` - log to standard out instead of a file. Heroku enables -this automatically. It is often nice in development as well. - `SENTRY_DSN` - set to your project sentry key to enable exception logging `SENTRY_ENV` - Sentry environment for the application. Defaults to 'unknown' if unset. From 85f6f29e2bfd8166cd4c63cb62e069968104ebc3 Mon Sep 17 00:00:00 2001 From: Isra Jazairi Date: Fri, 2 Oct 2026 09:20:55 -0700 Subject: [PATCH 3/3] Address code review feedback --- Gemfile | 1 - Gemfile.lock | 12 ------------ README.md | 2 +- config/environments/development.rb | 3 ++- 4 files changed, 3 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index 60c8e6de..376752eb 100644 --- a/Gemfile +++ b/Gemfile @@ -30,7 +30,6 @@ gem 'omniauth-saml' gem 'paper_trail' gem 'puma' gem 'rails', '~> 8.1.0' -gem 'rails_cloudflare_turnstile' gem 'rails_semantic_logger' gem 'rubyzip' gem 'sass-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 732818db..f298f9c3 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -175,12 +175,6 @@ GEM erb (6.0.7) erubi (1.13.1) execjs (2.10.1) - faraday (2.14.4) - faraday-net_http (>= 2.0, < 3.5) - json - logger - faraday-net_http (3.4.4) - net-http (~> 0.5) ffi (1.17.3) flipflop (2.8.0) activesupport (>= 4.0) @@ -252,8 +246,6 @@ GEM minitest (>= 5.0, < 7) ruby-progressbar msgpack (1.8.4) - net-http (0.9.1) - uri (>= 0.11.1) net-imap (0.6.6) date net-protocol @@ -330,9 +322,6 @@ GEM 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-mermaid_erd (0.8.0) rails (>= 5.2) - rails_cloudflare_turnstile (0.5.1) - faraday (>= 1.0, < 3.0) - rails (>= 6.0, < 8.2) rails_semantic_logger (5.2.0) rack railties (>= 7.2) @@ -518,7 +507,6 @@ DEPENDENCIES puma rails (~> 8.1.0) rails-mermaid_erd - rails_cloudflare_turnstile rails_semantic_logger rubocop rubocop-rails diff --git a/README.md b/README.md index 71d1d2a9..01ed3c97 100644 --- a/README.md +++ b/README.md @@ -92,9 +92,9 @@ public post key to enable capturing javascript exceptions. `RAILS_LOG_LEVEL` - we set sane defaults in development or production, but you can override easily with this ENV if you need to get more details. - `RAILS_APP_NAME` - overrides the application name used by semantic logger in structured log output. +`SEMANTIC_LOGGER_MULTILINE` - enables multiline semantic logger output in staging. `MAINTENANCE_MODE` - this toggles a [Flipflop feature](https://github.com/voormedia/flipflop) that disables thesis transfers and informs transfer submitters that the application is under maintenance. diff --git a/config/environments/development.rb b/config/environments/development.rb index 08feba27..3fe7e8df 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -89,9 +89,10 @@ config.log_level = ENV.fetch("RAILS_LOG_LEVEL", "debug") # Configure Rails Semantic Logger + multiline = ENV.fetch("SEMANTIC_LOGGER_MULTILINE", "true").downcase == "true" config.rails_semantic_logger.appenders do |appenders| # appenders.add(file_name: "log/#{Rails.env}.log", formatter: :color) # uncomment to log to file - appenders.add(io: $stdout, formatter: :color) + appenders.add(io: $stdout, formatter: { color: { ap: { multiline: multiline }}}) end # Annotate rendered view with file names.