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

Enable subclasses to configure level isolation #103

Merged
merged 1 commit into from
Nov 7, 2024

Conversation

skipkayhil
Copy link
Contributor

Logger#with_level was recently added to enable configuring a Logger's level for the duration of a block. However, the configured level is always tied to the currently running Fiber, which is not always ideal in applications that mix Threads and Fibers.

For example, Active Support has provided a similar feature (ActiveSupport::Logger#log_at) which, depending on configuration, can be isolated to either Threads or Fibers.

This commit enables subclasses of Logger to customize the level isolation. Ideally, it will enable replacing most of Active Support's #log_at, since both methods end up serving the same purpose.

Comment on lines +79 to +80
Fiber.new do
assert_equal(logger.level, DEBUG)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is mostly the same test as above, however, inside the Fiber the level does not get reset to INFO

@ioquatix
Copy link
Member

ioquatix commented Nov 6, 2024

This looks reasonable to me.

@jeremyevans do you mind reviewing it too?

Copy link
Contributor

@jeremyevans jeremyevans left a comment

Choose a reason for hiding this comment

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

I agree with the general idea. I found one change which seems to be an error. Once that is fixed, I think this is good to merge.

lib/logger.rb Outdated
@@ -426,7 +426,7 @@ def with_level(severity)
# Argument +datetime_format+ should be either of these:
#
# - A string suitable for use as a format for method
# {Time#strftime}[https://docs.ruby-lang.org/en/master/Time.html#method-i-strftime].
# {Time#strftime}[level_key].
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks wrong.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! I should have reviewed the diff more closely 😅

`Logger#with_level` was recently added to enable configuring a
`Logger`'s level for the duration of a block. However, the configured
level is always tied to the currently running `Fiber`, which is not
always ideal in applications that mix `Thread`s and `Fiber`s.

For example, Active Support has provided a similar feature
(`ActiveSupport::Logger#log_at`) which, depending on configuration, can
be isolated to either `Thread`s or `Fiber`s.

This commit enables subclasses of `Logger` to customize the level
isolation. Ideally, it will enable replacing most of Active Support's
`#log_at`, since both methods end up serving the same purpose.
@skipkayhil skipkayhil force-pushed the hm-configurable-level-key branch from d534fe6 to 8c9c928 Compare November 6, 2024 22:50
@ioquatix ioquatix merged commit dae2b83 into ruby:master Nov 7, 2024
24 checks passed
@ioquatix
Copy link
Member

ioquatix commented Nov 7, 2024

I also approve with this change.

matzbot pushed a commit to ruby/ruby that referenced this pull request Nov 7, 2024
(ruby/logger#103)

`Logger#with_level` was recently added to enable configuring a
`Logger`'s level for the duration of a block. However, the configured
level is always tied to the currently running `Fiber`, which is not
always ideal in applications that mix `Thread`s and `Fiber`s.

For example, Active Support has provided a similar feature
(`ActiveSupport::Logger#log_at`) which, depending on configuration, can
be isolated to either `Thread`s or `Fiber`s.

This commit enables subclasses of `Logger` to customize the level
isolation. Ideally, it will enable replacing most of Active Support's
`#log_at`, since both methods end up serving the same purpose.

ruby/logger@dae2b832cd
@skipkayhil skipkayhil deleted the hm-configurable-level-key branch November 11, 2024 01:26
@skipkayhil
Copy link
Contributor Author

Thank you both so much for the quick review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants