Repository navigation
Make framework log subscribers to consume structured events - #55900
Conversation
d1d2f2e to
1bec302
Compare
609b1f4 to
8b810c8
Compare
…seful for logging Related to #55900.
|
Shiped #55904 before this one. |
cecbd80 to
3493a2b
Compare
| CYAN = "\e[36m" | ||
| WHITE = "\e[37m" | ||
|
|
||
| mattr_accessor :colorize_logging, default: true |
There was a problem hiding this comment.
Isn't this breaking API change?
Since mattr_accessor are not picked up by RDoc, it is not visible in the public API docs however:
https://api.rubyonrails.org/classes/ActiveSupport/LogSubscriber.html
I did find one case of it in the wild, when running rails_semantic_logger tests against this branch:
amazing-print/amazing_print@main...zzak:amazing_print:rails-log-subscribers-colorize_logging
There was a problem hiding this comment.
I thought it wasn't because it wasn't visible in docs. I would opt to have private API users update their own code, but I'm open to suggestions. If someone else agrees, we can add deprecations, but I don't think it is necessary.
There was a problem hiding this comment.
I thought it wasn't because it wasn't visible in docs.
This is the general rule, yes so I think we're fine. The public API which is documented is through configuration:
https://edgeapi.rubyonrails.org/classes/Rails/Application/Configuration.html#method-i-colorize_logging
But I think that using mattr_* is intentionally meant to be private is false, that is just a side-effect of missing RDoc support.
There was a problem hiding this comment.
This breaks Rails projects that use Awesome Print (or its maintained fork Amazing Print Edit: Amazing Print has been fixed):
# lib/amazing_print/ext/active_support.rb
if defined?(ActiveSupport::LogSubscriber)
AwesomePrint.force_colors! ActiveSupport::LogSubscriber.colorize_logging
endWhile using the public ActiveSupport.colorize_logging API can be a quick fix for Amazing Print (the maintainers are pretty responsive), Awesome Print has been stale for years and many projects probably still depend on it (my current company does). So upgrading to Rails 8.2+ would mean requiring some companies to switch to amazing_print.
I understand this is possibly a private API, although there may be some ambiguity, and it is used by a few OSS projects according to https://sourcegraph.com/search?q=context:global+%22ActiveSupport::LogSubscriber.colorize_logging%22&patternType=keyword&sm=0. Not that many overall, but notables ones are awesome print, amazing print, a profiler class in the main Gitlab repo, a spec helper in https://github.com/rsim/oracle-enhanced, and a few other projects with 100+ github stars.
So I am wondering if the change is fine as is, or if this warrants a deprecation, a mention in the upgrade guides, or another course of action.
Edit 1: it's not just external gems, but rails/kredis also uses ActiveSupport::LogSubscriber.colorize_logging in one of its tests: rails/kredis@a6643a0/test/log_subscriber_test.rb#L12
Edit 2: I have submitted a fix for Amazing Print.
|
Tried this branch with If you unsubscribe from these do other things break, either intentionally or unintentionally. 🤔 Lograge also does something like this: Not sure how actionable it is, just came to me when thinking about this and thought it might be valuable context. 🙏 |
3493a2b to
2117e11
Compare
|
Most log subscribers are nodoc. I made all log subscribers nodoc in #55755, and if that is released in 8.1, and this in 8.2, that should be sufficient IMO. |
In particular, these two pull requests: - rails/rails#55897 - rails/rails#55900
This is a follow-up to rails#55657, a PR that enables debug events in development only. The problem is that some users might want to see debug events also in test or in production. A real-world example is detailed in rails#55900 (comment) This PR slightly changes the behavior replacing `Rails.env.development?` with `config.log_level == "debug"`. As explained in [Rails documentation](https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels): > The default Rails log level is :debug. However, it is set to :info for the production environment > in the default generated config/environments/production.rb. As a result, this PR maintains the existing behavior when defaults are applied, but opens up the possibility to see debug events in production for those who want to do so: they simply have to set `RAILS_LOG_LEVEL=debug`.
This is a follow-up to rails#55657, a PR that enables debug events in development only. The problem is that some users might want to see debug events also in test or in production. A real-world example is detailed in rails#55900 (comment) This PR slightly changes the behavior replacing `Rails.env.development?` with `config.log_level == "debug"`. As explained in [Rails documentation](https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels): > The default Rails log level is :debug. However, it is set to :info for the production environment > in the default generated config/environments/production.rb. As a result, this PR maintains the existing behavior when defaults are applied, but opens up the possibility to see debug events in production for those who want to do so: they simply have to set `RAILS_LOG_LEVEL=debug`.
This is a follow-up to rails#55657, a PR that enables debug events in development only. The problem is that some users might want to see debug events also in test or in production. A real-world example is detailed in rails#55900 (comment) This PR slightly changes the behavior replacing `Rails.env.development?` with `config.log_level == "debug"`. As explained in [Rails documentation](https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels): > The default Rails log level is :debug. However, it is set to :info for the production environment > in the default generated config/environments/production.rb. As a result, this PR maintains the existing behavior when defaults are applied, but opens up the possibility to see debug events in production for those who want to do so: they simply have to set `RAILS_LOG_LEVEL=debug`.
This is a follow-up to rails#55657, a PR that enables debug events in development only. The problem is that some users might want to see debug events also in test or in production. A real-world example is detailed in rails#55900 (comment) This PR slightly changes the behavior replacing `Rails.env.development?` with `config.log_level == "debug"`. As explained in [Rails documentation](https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels): > The default Rails log level is :debug. However, it is set to :info for the production environment > in the default generated config/environments/production.rb. As a result, this PR maintains the existing behavior when defaults are applied, but opens up the possibility to see debug events in production for those who want to do so: they simply have to set `RAILS_LOG_LEVEL=debug`.
This is a follow-up to rails#55657, a PR that enables debug events in development only. The problem is that some users might want to see debug events also in test or in production. A real-world example is detailed in rails#55900 (comment) This PR slightly changes the behavior replacing `Rails.env.development?` with `config.log_level == "debug"`. As explained in [Rails documentation](https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels): > The default Rails log level is :debug. However, it is set to :info for the production environment > in the default generated config/environments/production.rb. As a result, this PR maintains the existing behavior when defaults are applied, but opens up the possibility to see debug events in production for those who want to do so: they simply have to set `RAILS_LOG_LEVEL=debug`.
This is a follow-up to rails#55657, a PR that enables debug events in development only. The problem is that some users might want to see debug events also in test or in production. A real-world example is detailed in rails#55900 (comment) This PR slightly changes the behavior replacing `Rails.env.development?` with `config.log_level == "debug"`. As explained in [Rails documentation](https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels): > The default Rails log level is :debug. However, it is set to :info for the production environment > in the default generated config/environments/production.rb. As a result, this PR maintains the existing behavior when defaults are applied, but opens up the possibility to see debug events in production for those who want to do so: they simply have to set `RAILS_LOG_LEVEL=debug`.
This is a follow-up to rails#55657, a PR that enables debug events in development only. The problem is that some users might want to see debug events also in test or in production. A real-world example is detailed in rails#55900 (comment) This PR slightly changes the behavior replacing `Rails.env.development?` with `config.log_level == "debug"`. As explained in [Rails documentation](https://guides.rubyonrails.org/debugging_rails_applications.html#log-levels): > The default Rails log level is :debug. However, it is set to :info for the production environment > in the default generated config/environments/production.rb. As a result, this PR maintains the existing behavior when defaults are applied, but opens up the possibility to see debug events in production for those who want to do so: they simply have to set `RAILS_LOG_LEVEL=debug`.
Following rails#55900, we now always pay the efty price of building the event payload and filtering it before we check subscribers to know if they even need it. I realize this is technically a breaking change, but the filtering logic seem to have been ported over from AS::Notifications but in AS::Notifications we'd save building the payload, here registering a filter doesn't allow to save any substantial overhead. So in my opinion the feature doesn't make sense as is.
Following rails#55900, we now always pay the efty price of building the event payload and filtering it before we check subscribers to know if they even need it. I realize this is technically a breaking change, but the filtering logic seem to have been ported over from AS::Notifications but in AS::Notifications we'd save building the payload, here registering a filter doesn't allow to save any substantial overhead. So in my opinion the feature doesn't make sense as is.
Following rails#55900, we now always pay the efty price of building the event payload and filtering it before we check subscribers to know if they even need it. I realize this is technically a breaking change, but the filtering logic seem to have been ported over from AS::Notifications but in AS::Notifications we'd save building the payload, here registering a filter doesn't allow to save any substantial overhead. So in my opinion the feature doesn't make sense as is.
In Rails 8.2, ActiveRecord::LogSubscriber was refactored to use ActiveSupport::EventReporter instead of ActiveSupport::Notifications (rails/rails#55900). Adapt LoggerSpecHelper for the new logging pipeline: 1. Remove ActiveSupport::LogSubscriber.colorize_logging= (removed in Rails 8.2). Use ActiveSupport.colorize_logging instead, saving and restoring the previous value. 2. Remove ActiveRecord::LogSubscriber.attach_to(:active_record) — the new EventReporter::LogSubscriber base class does not have attach_to. 3. Remove notifier swapping from set_logger/clear_logger. The subscriber is now registered via EventReporter at load time and writes to ActiveRecord::Base.logger, so just swapping the logger is sufficient. 4. Remove the unused wait helper. Co-Authored-By: Claude Opus 4.6 <[email protected]>
In Rails 8.2, SQL logging was refactored with a new pipeline: Notifications → StructuredEventSubscriber → EventReporter → LogSubscriber (rails/rails#55690, rails/rails#55900). Both StructuredEventSubscriber and LogSubscriber have their own IGNORE_PAYLOAD_NAMES = ["SCHEMA", "EXPLAIN"]. The existing spec_helper only patched LogSubscriber's copy, but SCHEMA queries are now filtered upstream at StructuredEventSubscriber before they ever reach LogSubscriber. Since all oracle-enhanced schema methods (table_comment, column_comment, foreign_keys, column_definitions, indexes, etc.) use "SCHEMA" as the query name, none of their SQL appeared in the test logger, causing 12 test failures. Co-Authored-By: Claude Opus 4.6 <[email protected]>
Motivation / Background
This Pull Request has been created because framework log subscribers currently consume notification events. Since the introduction of structured event subscribers in #55690, this doesn't necessarily need to happen anymore. We can instead use the structured event subscribers to produce logs instead for environments like development.
Detail
This Pull Request adds a base class for framework structured event log subscribers, and changes current log subscribers (private API since #55755) to consume structured events instead of notification events.
Checklist
Before submitting the PR make sure the following are checked:
[Fix #issue-number]