From 8baaa1ed786c3ee783feb2f7de212fcdd94d200e Mon Sep 17 00:00:00 2001 From: Colin Saliceti Date: Tue, 28 May 2024 15:18:21 +0100 Subject: [PATCH 1/4] Stop okcomputer logging It creates 5 log entries for each healthcheck, which is currently 1 per second on kubernetes. But this is not that useful. --- config/initializers/okcomputer.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/config/initializers/okcomputer.rb b/config/initializers/okcomputer.rb index 45b34e2b7..f7807c7ee 100644 --- a/config/initializers/okcomputer.rb +++ b/config/initializers/okcomputer.rb @@ -1,7 +1,6 @@ require_relative "../../lib/ok_computer_checks/zendesk_check" require_relative "../../lib/ok_computer_checks/notify_check" -OkComputer.logger = Rails.logger OkComputer.mount_at = "health" OkComputer::Registry.register "postgresql", OkComputer::ActiveRecordCheck.new From e6142459094e4e9697d2ff1ba45f5aad6ffa167d Mon Sep 17 00:00:00 2001 From: Malcolm Baig Date: Wed, 29 May 2024 11:43:18 +0000 Subject: [PATCH 2/4] Add Semantic Logger Based on these docs: https://technical-guidance.education.gov.uk/infrastructure/monitoring/logit/#new-stack --- Gemfile | 4 ++++ Gemfile.lock | 7 +++++++ config/environments/production.rb | 5 +++++ config/initializers/semantic_logger.rb | 7 +++++++ 4 files changed, 23 insertions(+) create mode 100644 config/initializers/semantic_logger.rb diff --git a/Gemfile b/Gemfile index 54d9a08c9..ef912858b 100644 --- a/Gemfile +++ b/Gemfile @@ -94,3 +94,7 @@ group :test do gem "vcr", "~> 6.2" gem "webmock", "~> 3.18" end + +group :production do + gem "rails_semantic_logger" +end diff --git a/Gemfile.lock b/Gemfile.lock index 329eb74f6..1c26a55d9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -349,6 +349,10 @@ GEM rails-html-sanitizer (1.6.0) loofah (~> 2.21) nokogiri (~> 1.14) + rails_semantic_logger (4.14.0) + rack + railties (>= 5.1) + semantic_logger (~> 4.13) railties (7.1.3.1) actionpack (= 7.1.3.1) activesupport (= 7.1.3.1) @@ -435,6 +439,8 @@ GEM rexml ruby-progressbar (1.13.0) ruby2_keywords (0.0.5) + semantic_logger (4.15.0) + concurrent-ruby (~> 1.0) sentry-rails (5.10.0) railties (>= 5.0) sentry-ruby (~> 5.10.0) @@ -580,6 +586,7 @@ DEPENDENCIES rack-attack rails (~> 7.1.3) rails-erd + rails_semantic_logger rladr (~> 1.2) rspec rspec-rails diff --git a/config/environments/production.rb b/config/environments/production.rb index b08fdffd0..9cf3348ea 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -96,4 +96,9 @@ config.action_mailer.notify_settings = { api_key: ENV.fetch("GOVUK_NOTIFY_API_KEY"), } + + config.log_level = :info # Or :warn, or :error + config.log_format = :json # For parsing in Logit + config.rails_semantic_logger.add_file_appender = false # Don't log to file + config.active_record.logger = nil # Don't log SQL end diff --git a/config/initializers/semantic_logger.rb b/config/initializers/semantic_logger.rb new file mode 100644 index 000000000..19877b879 --- /dev/null +++ b/config/initializers/semantic_logger.rb @@ -0,0 +1,7 @@ +Rails.application.configure do + config.semantic_logger.application = "" # This is added by logstash from its tags + config.log_tags = [:request_id] # Prepend all log lines with the following tags +end + +SemanticLogger.add_appender(io: STDOUT, level: Rails.application.config.log_level, formatter: Rails.application.config.log_format) +Rails.application.config.logger.info('Application logging to STDOUT') From 29721a2f2d8bc3c04bde819c967ad5250c88486d Mon Sep 17 00:00:00 2001 From: Malcolm Baig Date: Wed, 29 May 2024 11:48:07 +0000 Subject: [PATCH 3/4] Don't run Semantic Logger initializer unless gem is in use We're only using Semantic Logger in production, so return early from the initializer if the constant defined by the gem is missing. --- config/initializers/semantic_logger.rb | 2 ++ 1 file changed, 2 insertions(+) diff --git a/config/initializers/semantic_logger.rb b/config/initializers/semantic_logger.rb index 19877b879..b3cf5648d 100644 --- a/config/initializers/semantic_logger.rb +++ b/config/initializers/semantic_logger.rb @@ -1,3 +1,5 @@ +return unless defined?(SemanticLogger) + Rails.application.configure do config.semantic_logger.application = "" # This is added by logstash from its tags config.log_tags = [:request_id] # Prepend all log lines with the following tags From 0abfd83b6e2a882ea411921b981fe4eb87e74ad7 Mon Sep 17 00:00:00 2001 From: Malcolm Baig Date: Wed, 29 May 2024 12:40:19 +0000 Subject: [PATCH 4/4] Fix linting error --- config/initializers/semantic_logger.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/config/initializers/semantic_logger.rb b/config/initializers/semantic_logger.rb index b3cf5648d..2a1c2785d 100644 --- a/config/initializers/semantic_logger.rb +++ b/config/initializers/semantic_logger.rb @@ -5,5 +5,6 @@ config.log_tags = [:request_id] # Prepend all log lines with the following tags end -SemanticLogger.add_appender(io: STDOUT, level: Rails.application.config.log_level, formatter: Rails.application.config.log_format) +SemanticLogger.add_appender(io: $stdout, level: Rails.application.config.log_level, +formatter: Rails.application.config.log_format) Rails.application.config.logger.info('Application logging to STDOUT')