Skip to content

Commit

Permalink
Merge pull request #1828 from MushroomObserver/nimmo-log-level-7.1.3
Browse files Browse the repository at this point in the history
Monkey patch `ActionView::LogSubscriber`
  • Loading branch information
nimmolo committed Jan 19, 2024
2 parents 999b3f8 + 8f4cb4a commit be54ecd
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
18 changes: 18 additions & 0 deletions config/initializers/monkey_patches.rb
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
40 changes: 40 additions & 0 deletions lib/core_extensions/action_view/log_subscriber.rb
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

0 comments on commit be54ecd

Please sign in to comment.