diff --git a/Gemfile b/Gemfile index 8a6513fd..84373293 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,9 @@ gem 'tzinfo-data', platforms: %i[windows jruby] gem 'rack-cors' +# Replace default logger with semantic logging +gem 'rails_semantic_logger' + # Use Redis adapter to run Action Cable in production # gem "redis", ">= 4.0.1" @@ -108,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/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/environments/development.rb b/config/environments/development.rb index 09ebac9a..51f66ee8 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| + multiline = ENV.fetch("SEMANTIC_LOGGER_MULTILINE", "true").downcase == "true" + appenders.add_server( + formatter: {color: {ap: {multiline: multiline}}} + ) + end + # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log @@ -70,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 b84de475..6efc11ad 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -39,10 +39,11 @@ # 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.logger = ActiveSupport::Logger.new(STDOUT) - .tap { |logger| logger.formatter = ::Logger::Formatter.new } - .then { |logger| ActiveSupport::TaggedLogging.new(logger) } + # 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 # Log to STDOUT with the current request id as a default log tag. config.log_tags = [ :request_id ]