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

Conversation

tunylund
Copy link
Contributor

@tunylund tunylund commented Jul 17, 2023

Rails libraries expect the Rails.logger instance to inherit from ActiveSupport::Logger. The ruby standard library logger for example does not implement methods like silence which is used by some of the Rails internals.

This change fixes the inheritance issue.

Co-authored-by: laura.ghiorghisor@digital.cabinet-office.gov.uk

This fixes the below error:
Screenshot 2023-07-17 at 11 04 05

The error appears in development environment when Production like logging is enabled.

Tested with happy results:
image

Copy link
Member

@kevindew kevindew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch on the issue, I've made a couple of suggestions

CHANGELOG.md Outdated
@@ -1,3 +1,7 @@
# 9.0.4

* Fix an issue of using Production like logs in development environment.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really tell users much about the change or the issue. Would suggest something like: Fix Rails logger not being a descendent of ActiveSupport::Logger and thus lacking expected methods.

It'd also be good to follow the convention of linking to the PR that introduced the changelog (some are gems are very consistent with this but this one seems to have become a bit loose)

@@ -26,6 +26,7 @@ def self.configure
"#{hash.to_json}\n"
},
)
Rails.logger = ActiveSupport::TaggedLogging.new(logger)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit of a surprise to me that we use ActiveSupport::TaggedLogging here since the commit only talks about ActiveSupport::Logger. I would be more expecting that the change was: Rails.logger = ActiveSupport::Logger.new(

Couple of things I wonder:
a) do we actually need the tagged logger? if so can we include the reasons for that in the commit, if not shall we simplify to just ActiveSupport::Logger?
b) if someone was to actually set a tag logger.tag("test") { logger.warn("hi") } might that actually break the JSON output or does it just prefix the message (hopefully the latter, just determining if tagging is dangerous)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah totally agree, we ended up using ActiveSupport::TaggedLoggging because ActiveSupport::Logger.new() didn't immediately work as expected. I had another playaround with it and realized that the formatter needs to be set separately instead of part of the keyword arguments in order for it to work. The new version uses ActiveSupport::Logger instead of TaggedLogging.

Tuomas Nylund added 2 commits July 17, 2023 14:08
…plication logger

Rails libraries expect the `Rails.logger` instance to inherit from ActiveSupport::Logger.
The standard library logger for example does not implement methods like `silence` which is
used by some of the Rails internals.

This change fixes the inheritance issue.

Co-authored-by: laura.ghiorghisor@digital.cabinet-office.gov.uk
Copy link
Member

@kevindew kevindew left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - LGTM.

Have you given it a try in an app just to confirm it behaves as expected prior to releasing the gem?

@tunylund
Copy link
Contributor Author

yeah tested the latest changes as well, all good 👍

@tunylund tunylund merged commit e5edc33 into main Jul 17, 2023
5 checks passed
@tunylund tunylund deleted the fix-logger-implementation branch July 17, 2023 15:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants