Skip to content

Conversation

@gmcgibbon
Copy link
Member

@gmcgibbon gmcgibbon commented Oct 14, 2025

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.

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

Copy link
Contributor

@jeromedalbert jeromedalbert Oct 24, 2025

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):

# 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 would be a quick fix for Amazing Print (I recently contributed there and the maintainers are responsive), Awesome Print has been stale for years and many projects probably still depend on it. So upgrading to Rails 8.2+ would mean requiring companies to switch to amazing_print.

I understand this is possibly a private API, but are we okay with proceeding as is? Or is this possibly private API used be enough "high profile" gems to warrant a deprecation or a mention in the upgrade guides?

The most "high profiles" result I see from https://sourcegraph.com/search?q=context:global+%22ActiveSupport::LogSubscriber.colorize_logging%22&patternType=keyword&sm=0 are awesome print, amazing print, a profiler in the gitlab repo.

@zzak
Copy link
Member

zzak commented Oct 16, 2025

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

@gmcgibbon
Copy link
Member Author

I'll backport the more recent structured logging additions here to 8.1 stable so that we can include that in the full release (or another RC). Looks like the pressure is off to get this out before 8.1, and its a pretty big change, so I'll leave it open for a few more days for feedback.

gmcgibbon added a commit to Shopify/rails that referenced this pull request Oct 16, 2025
@gmcgibbon gmcgibbon force-pushed the new_log_subscribers branch 2 times, most recently from 5b73cee to 4f9b7d3 Compare October 22, 2025 04:10
@gmcgibbon gmcgibbon force-pushed the new_log_subscribers branch from 4f9b7d3 to b601abc Compare October 22, 2025 04:21
@gmcgibbon gmcgibbon force-pushed the new_log_subscribers branch from b601abc to d8977f2 Compare October 22, 2025 16:19
@gmcgibbon gmcgibbon force-pushed the new_log_subscribers branch from d8977f2 to b646f35 Compare October 22, 2025 16:29
@gmcgibbon gmcgibbon merged commit 54264bd into rails:main Oct 23, 2025
4 checks passed
@gmcgibbon gmcgibbon deleted the new_log_subscribers branch October 23, 2025 16:07

module ActiveSupport
class EventReporter
class LogSubscriber
Copy link
Contributor

Choose a reason for hiding this comment

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

Since there is a CHANGELOG entry for this class, should we add some rdoc documentation?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I'll put this on my TODO list, but PRs are welcome in the meantime!

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.

6 participants