From feed7f209c57e53e61c4f988d00f368fe14152ac Mon Sep 17 00:00:00 2001 From: vangberg Date: Thu, 18 Jun 2026 10:20:37 +0200 Subject: [PATCH] Use semantic JSON logging in production --- Gemfile | 1 + Gemfile.lock | 7 +++++++ config/application.rb | 3 +++ config/environments/production.rb | 14 ++++---------- 4 files changed, 15 insertions(+), 10 deletions(-) diff --git a/Gemfile b/Gemfile index 33b7678b..6cc3e289 100644 --- a/Gemfile +++ b/Gemfile @@ -35,6 +35,7 @@ gem 'puma', '~> 5.6' gem 'rails', '~> 6.1' gem 'rack-attack' gem 'rack-cors' +gem 'rails_semantic_logger', '~> 4.20' gem 'redcarpet', '~> 3.5' gem 'redirect_safely', '~> 1.0' gem 'rexml' diff --git a/Gemfile.lock b/Gemfile.lock index 6e982439..728495a1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -286,6 +286,10 @@ GEM rails-html-sanitizer (1.7.0) loofah (~> 2.25) 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 (4.20.0) + rack + railties (>= 5.1) + semantic_logger (~> 4.16) railties (6.1.7.10) actionpack (= 6.1.7.10) activesupport (= 6.1.7.10) @@ -382,6 +386,8 @@ GEM rexml (~> 3.2, >= 3.2.5) rubyzip (>= 1.2.2, < 4.0) websocket (~> 1.0) + semantic_logger (4.18.0) + concurrent-ruby (~> 1.0) semantic_range (3.1.1) serrano (1.7) faraday (~> 2.14, >= 2.14.1) @@ -499,6 +505,7 @@ DEPENDENCIES rack-attack rack-cors rails (~> 6.1) + rails_semantic_logger (~> 4.20) rdoc redcarpet (~> 3.5) redirect_safely (~> 1.0) diff --git a/config/application.rb b/config/application.rb index d3bd5efb..d0f527ab 100644 --- a/config/application.rb +++ b/config/application.rb @@ -14,6 +14,9 @@ class Application < Rails::Application # Boolean handling of SQLite3 config.active_record.sqlite3&.represent_boolean_as_integer = true + config.semantic_logger.application = 'seqcode-registry' + config.semantic_logger.environment = Rails.env + # Settings in config/environments/* take precedence over those specified here. # Application configuration can go into files in config/initializers # -- all .rb files in that directory are automatically loaded after loading diff --git a/config/environments/production.rb b/config/environments/production.rb index 07aa50a4..d15d322f 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -56,7 +56,7 @@ config.log_level = :warn # Prepend all log lines with the following tags. - config.log_tags = [ :request_id ] + config.log_tags = { request_id: :request_id } # Use a different cache store in production. # config.cache_store = :mem_cache_store @@ -91,17 +91,11 @@ # Send deprecation notices to registered listeners. config.active_support.deprecation = :notify - # Use default logging formatter so that PID and timestamp are not suppressed. - config.log_formatter = ::Logger::Formatter.new - - # Use a different logger for distributed setups. - # require 'syslog/logger' - # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + config.rails_semantic_logger.format = :json if ENV['RAILS_LOG_TO_STDOUT'].present? - logger = ActiveSupport::Logger.new(STDOUT) - logger.formatter = config.log_formatter - config.logger = ActiveSupport::TaggedLogging.new(logger) + config.rails_semantic_logger.add_file_appender = false + config.semantic_logger.add_appender(io: STDOUT, formatter: :json) end # Do not dump schema after migrations.