Skip to content

Make framework log subscribers to consume structured events - #55900

Merged
gmcgibbon merged 7 commits into
rails:mainfrom
Shopify:new_log_subscribers
Oct 23, 2025
Merged

gmcgibbon merged 7 commits into
rails:mainfrom
Shopify:new_log_subscribers

Conversation

@gmcgibbon

@gmcgibbon gmcgibbon commented Oct 14, 2025 •

Copy link
Copy Markdown
Member

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:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

Comment thread actionview/lib/action_view/railtie.rb Resolved
Comment thread activesupport/CHANGELOG.md Resolved
@rafaelfranca rafaelfranca added this to the 8.1.0 milestone Oct 14, 2025
@gmcgibbon
gmcgibbon force-pushed the new_log_subscribers branch 2 times, most recently from 609b1f4 to 8b810c8 Compare October 14, 2025 22:24
@rafaelfranca

Copy link
Copy Markdown
Member

Shiped #55904 before this one.

@rafaelfranca rafaelfranca modified the milestones: 8.1.0, 8.2.0 Oct 14, 2025
@gmcgibbon
gmcgibbon force-pushed the new_log_subscribers branch 8 times, most recently from cecbd80 to 3493a2b Compare October 16, 2025 07:24
CYAN = "\e[36m"
WHITE = "\e[37m"

mattr_accessor :colorize_logging, default: true

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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.

@jeromedalbert jeromedalbert Oct 24, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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
end

Source: https://github.com/awesome-print/awesome_print/blob/8a7ff0aabacbebf694c3e27242809219a96d5a3b/lib/awesome_print/ext/active_support.rb#L44-L46

While 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.

@zzak

zzak commented Oct 16, 2025

Copy link
Copy Markdown
Member

Tried this branch with rails_semantic_logger tests and it seems they will have to change how they swap out subscribers, like:
reidmorrison/rails_semantic_logger@master...zzak:rails_semantic_logger:swap-structured_event_subscribers

If you unsubscribe from these do other things break, either intentionally or unintentionally. 🤔

Lograge also does something like this:
https://github.com/roidrage/lograge/blob/27066cd75d449cb91ed79b18c4271d62a76c9839/lib/lograge.rb#L109-L127

Not sure how actionable it is, just came to me when thinking about this and thought it might be valuable context. 🙏

@gmcgibbon
gmcgibbon force-pushed the new_log_subscribers branch from 3493a2b to 2117e11 Compare October 16, 2025 20:40
@gmcgibbon

Copy link
Copy Markdown
Member Author

class LogSubscriber < ActiveSupport::LogSubscriber # :nodoc:

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.

bquorning added a commit to zendesk/active_record_host_pool that referenced this pull request Nov 11, 2025
In particular, these two pull requests:

- rails/rails#55897
- rails/rails#55900
bquorning added a commit to zendesk/active_record_host_pool that referenced this pull request Nov 11, 2025
bquorning added a commit to zendesk/active_record_host_pool that referenced this pull request Nov 11, 2025
claudiob added a commit to claudiob/rails that referenced this pull request Nov 22, 2025
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`.
claudiob added a commit to claudiob/rails that referenced this pull request Nov 22, 2025
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`.
claudiob added a commit to claudiob/rails that referenced this pull request Nov 22, 2025
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`.
claudiob added a commit to claudiob/rails that referenced this pull request Nov 25, 2025
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`.
claudiob added a commit to claudiob/rails that referenced this pull request Nov 25, 2025
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`.
claudiob added a commit to claudiob/rails that referenced this pull request Nov 27, 2025
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`.
@jasl jasl mentioned this pull request Dec 7, 2025
1 of 3 tasks
drymar pushed a commit to drymar/rails that referenced this pull request Jan 5, 2026
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`.
byroot added a commit to byroot/rails that referenced this pull request Feb 10, 2026
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.
byroot added a commit to byroot/rails that referenced this pull request Feb 11, 2026
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.
nicolasva pushed a commit to nicolasva/rails that referenced this pull request Mar 12, 2026
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.
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
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]>
yahonda added a commit to yahonda/oracle-enhanced that referenced this pull request Apr 10, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants