-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1828 from MushroomObserver/nimmo-log-level-7.1.3
Monkey patch `ActionView::LogSubscriber`
- Loading branch information
Showing
2 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# frozen_string_literal: true | ||
|
||
# https://dev.to/ayushn21/applying-monkey-patches-in-rails-1bj1 | ||
# https://binarysolo.chapter24.blog/applying-monkey-patches-in-rails/ | ||
# https://www.justinweiss.com/articles/3-ways-to-monkey-patch-without-making-a-mess/ | ||
require("action_view/log_subscriber") | ||
|
||
# Require all Ruby files in the core_extensions directory | ||
Dir[Rails.root.join("lib/core_extensions/**/*.rb")].each { |f| require f } | ||
|
||
# Does not work | ||
# ActiveSupport.on_load(:action_view) do | ||
# ActionView::LogSubscriber.include(CoreExtensions::ActionView::LogSubscriber) | ||
# end | ||
|
||
class ActionView::LogSubscriber | ||
prepend CoreExtensions::ActionView::LogSubscriber | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# frozen_string_literal: true | ||
|
||
# Overrides because we want these logged at info, not debug level | ||
module CoreExtensions | ||
module ActionView | ||
module LogSubscriber | ||
def render_collection(event) | ||
identifier = event.payload[:identifier] || "templates" | ||
|
||
info do | ||
message = +" Rendered collection of #{from_rails_root(identifier)}" | ||
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] | ||
message << " #{render_count(event.payload)} (Duration: #{event.duration.round(1)}ms | Allocations: #{event.allocations})" | ||
message | ||
end | ||
end | ||
# ::ActionView::LogSubscriber.subscribe_log_level :render_collection, :info | ||
|
||
def render_partial(event) | ||
if event.payload[:cache_hit].present? | ||
info do | ||
message = +" Rendered #{from_rails_root(event.payload[:identifier])}" | ||
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] | ||
message << " (Duration: #{event.duration.round(1)}ms | Allocations: #{event.allocations})" | ||
message << " #{cache_message(event.payload)}" unless event.payload[:cache_hit].nil? | ||
message | ||
end | ||
else | ||
debug do | ||
message = +" Rendered #{from_rails_root(event.payload[:identifier])}" | ||
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout] | ||
message << " (Duration: #{event.duration.round(1)}ms | Allocations: #{event.allocations})" | ||
message | ||
end | ||
end | ||
end | ||
# ::ActionView::LogSubscriber.subscribe_log_level :render_partial, :info | ||
end | ||
end | ||
end |