Skip to content

Commit

Permalink
Merge pull request #309 from alphagov/fix-logger-implementation
Browse files Browse the repository at this point in the history
Use ActiveSupport::Logger instead of Ruby std Logger for the Rails apps
  • Loading branch information
tunylund committed Jul 17, 2023
2 parents 8fab58b + cc9fdbd commit e5edc33
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 9.0.4

* Fix an issue with Rails.logger being not an instance of ActiveSupport::Logger. Rails expects Rails.logger to have methods that Ruby STD Logger does not provide. e.g. `silence()` ([#309](https://github.com/alphagov/govuk_app_config/pull/309))

# 9.0.3

* When error is reported by Rails logger, the field is now logged as "error_message" in order to avoid overwriting the "message" field.
Expand Down
29 changes: 13 additions & 16 deletions lib/govuk_app_config/govuk_json_logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,21 @@ def self.configure
# indefinitely while troubleshooting.
$stdout.sync = true

Rails.logger = Logger.new(
$stdout,
level: Rails.logger.level,
formatter: proc { |severity, datetime, _progname, msg|
hash = {
"@timestamp": datetime.utc.iso8601(3),
message: msg,
level: severity,
tags: %w[rails],
}
Rails.logger = ActiveSupport::Logger.new($stdout, level: Rails.logger.level)
Rails.logger.formatter = proc { |severity, datetime, _progname, msg|
hash = {
"@timestamp": datetime.utc.iso8601(3),
message: msg,
level: severity,
tags: %w[rails],
}

if defined?(GdsApi::GovukHeaders) && !GdsApi::GovukHeaders.headers[:govuk_request_id].nil?
hash[:govuk_request_id] = GdsApi::GovukHeaders.headers[:govuk_request_id]
end
if defined?(GdsApi::GovukHeaders) && !GdsApi::GovukHeaders.headers[:govuk_request_id].nil?
hash[:govuk_request_id] = GdsApi::GovukHeaders.headers[:govuk_request_id]
end

"#{hash.to_json}\n"
},
)
"#{hash.to_json}\n"
}

LogStasher.add_custom_fields do |fields|
# Mirrors Nginx request logging, e.g. GET /path/here HTTP/1.1
Expand Down
2 changes: 1 addition & 1 deletion lib/govuk_app_config/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module GovukAppConfig
VERSION = "9.0.3".freeze
VERSION = "9.0.4".freeze
end

0 comments on commit e5edc33

Please sign in to comment.