# Subscribe to an instrumentation.
ActiveSupport::Notifications.subscribe("wait") do |*args|
@event = ActiveSupport::Notifications::Event.new(*args)
end
# Instrument code to generate notifications.
payload = { data: "wait_event" }
ActiveSupport::Notifications.instrument("wait", payload) do
sleep 1
end
@event.duration # => ~1000ms
@event.payload[:data] => "wait_event"
Consume ActiveSupport::Notifications into logs. Speicalized for logs so you can use debug
, info
, warn
, error
, fatal
methods to log from your Rails.logger
.
Example, logs ActionCable’s perform_action
:
class ActionCablePerformActionLogSubscriber < ActiveSupport::LogSubscriber
def perform_action(event)
payload = event.payload
channel_action = "#{payload[:channel_class]}##{payload[:action]}"
if exception = event.payload[:exception_object]
error "[ActionCable] ERROR -- : #{channel_action} (Exception: #{exception.message})"
else
info "[ActionCable] INFO -- : #{channel_action} (Duration: #{event.duration})"
end
end
end
ActionCablePerformActionLogSubscriber.attach_to :action_cable
The event
is an instance of ActiveSupport::Notifications::Event
.
The event object generated from ActiveSupport::Notifications
. event object comes with a name
, payload
, which added from the second argument passed to ActiveSupport::Notifications.instrument
method.
event
has some convenient methods defined for the course of the event:
-
duration
(ms) allocations