Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ActiveSupport::Logger instead of Ruby std Logger for the Rails apps #309

Merged
merged 2 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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