From 0a6f772744fd268caeb1e5e8d923e4a6c310a977 Mon Sep 17 00:00:00 2001 From: Iris Lau Date: Wed, 21 Jun 2023 16:52:22 +0100 Subject: [PATCH] Remove the rewiring stdout and strerr logic The redirection was a workaround for log file parsers and it is no longer needed. So we remove that part and simplify the logger settings. Co-authored-by: Neamah Al-Selwi Co-authored-by: Tuomas Nylund --- README.md | 3 --- lib/govuk_app_config/govuk_json_logging.rb | 22 ++-------------------- spec/lib/govuk_json_logging_spec.rb | 10 +++++----- 3 files changed, 7 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index f67a311..6aaccab 100644 --- a/README.md +++ b/README.md @@ -142,9 +142,6 @@ check docs](docs/healthchecks.md) for more information on how to use it. ## Rails logging -In Rails applications, the application will be configured to send JSON-formatted -logs to `STDOUT` and unstructured logs to `STDERR`. - To enable production-like logging, an env variable `GOVUK_RAILS_JSON_LOGGING` is set in the `govuk-helm-charts` and then checked in `railtie.rb`. This will allow JSON format logs and `Govuk-Request-Id` to be visible. diff --git a/lib/govuk_app_config/govuk_json_logging.rb b/lib/govuk_app_config/govuk_json_logging.rb index 8693126..7b6fb0f 100644 --- a/lib/govuk_app_config/govuk_json_logging.rb +++ b/lib/govuk_app_config/govuk_json_logging.rb @@ -5,32 +5,14 @@ module GovukJsonLogging def self.configure - # GOV.UK Rails applications are expected to output JSON to stdout which is - # then indexed in a Kibana instance. These log outputs are created by the - # logstasher gem. - # - # Rails applications will typically write other things to stdout such as - # `Rails.logger` calls or 'puts' statements. However these are not in a - # JSON format which causes problems for the log file parsers. - # - # To resolve this we redirect stdout to stderr, to cover any Rails - # writing. This frees up the normal stdout for the logstasher logs. - # # We also disable buffering, so that logs aren't lost on crash or delayed # indefinitely while troubleshooting. - - # rubocop:disable Style/GlobalVars - $real_stdout = $stdout.clone - $real_stdout.sync = true - $stdout.reopen($stderr) $stdout.sync = true - # rubocop:enable Style/GlobalVars Rails.logger = Logger.new( - $real_stdout, # rubocop:disable Style/GlobalVars + $stdout, level: Rails.logger.level, formatter: proc { |severity, datetime, _progname, msg| - begin message = JSON.parse(msg) rescue JSON::ParserError, TypeError => _e @@ -77,7 +59,7 @@ def self.configure Rails.application.config.logstasher.source = {} Rails.application.config.logstasher.logger = Logger.new( - $real_stdout, # rubocop:disable Style/GlobalVars + $stdout, level: Rails.logger.level, formatter: proc { |_severity, _datetime, _progname, msg| "#{msg.is_a?(String) ? msg : msg.inspect}\n" diff --git a/spec/lib/govuk_json_logging_spec.rb b/spec/lib/govuk_json_logging_spec.rb index 519c173..25ac701 100644 --- a/spec/lib/govuk_json_logging_spec.rb +++ b/spec/lib/govuk_json_logging_spec.rb @@ -23,6 +23,7 @@ def self.headers after { Rails.application = nil } original_stderr = nil + original_stdout = nil let(:fake_stdout) { StringIO.new } let(:fake_stderr) { StringIO.new } @@ -30,15 +31,15 @@ def self.headers before do original_stderr = $stderr + original_stdout = $stdout $stderr = fake_stderr - allow($stdout).to receive(:clone).and_return(fake_stdout) - allow($stdout).to receive(:reopen) + $stdout = fake_stdout Rails.logger = Logger.new(fake_stdout, level: info_log_level) - end after do $stderr = original_stderr + $stdout = original_stdout end describe ".configure" do @@ -80,7 +81,7 @@ def app Rails.application end - it "logs errors thrown by the application" do + it "logs errors thrown by the application with govuk_request_id" do stub_const("GdsApi::GovukHeaders", govuk_headers_class) GovukJsonLogging.configure get "/error" @@ -133,7 +134,6 @@ def app expect(log_json).to include("message" => "test default log entry") expect(log_json).to include("govuk_request_id" => "some-value") - end end end