diff --git a/Gemfile b/Gemfile index 38d9df3d..376752eb 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,7 @@ gem 'omniauth-saml' gem 'paper_trail' gem 'puma' gem 'rails', '~> 8.1.0' +gem 'rails_semantic_logger' gem 'rubyzip' gem 'sass-rails' gem 'sentry-rails' @@ -55,6 +55,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..f298f9c3 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) @@ -222,11 +223,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) @@ -326,6 +322,10 @@ 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_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 +402,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 +470,7 @@ PLATFORMS DEPENDENCIES administrate (~> 1.0.0) + amazing_print annotaterb aws-sdk-rails aws-sdk-s3 @@ -490,7 +493,6 @@ DEPENDENCIES kaminari letter_opener listen - lograge marc minitest (< 6) minitest-reporters @@ -505,6 +507,7 @@ DEPENDENCIES puma rails (~> 8.1.0) rails-mermaid_erd + rails_semantic_logger rubocop rubocop-rails rubyzip diff --git a/README.md b/README.md index 80811c0b..01ed3c97 100644 --- a/README.md +++ b/README.md @@ -87,14 +87,14 @@ 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. +`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. @@ -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. diff --git a/config/environments/development.rb b/config/environments/development.rb index d9ccf12e..3fe7e8df 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -86,16 +86,13 @@ # 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 + 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: { ap: { multiline: multiline }}}) 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