class Sentry::Rails::LogSubscriber
end
end
)
}
custom_data: event.payload[:custom_data]
duration_ms: event.duration,
attributes: {
level: :info,
message: “My event occurred”,
log_structured_event(
def my_event(event)
attach_to :my_component
class MySubscriber < Sentry::Rails::LogSubscriber
@example Creating a custom log subscriber
structured logging system.
for capturing Rails instrumentation events and logging them through Sentry’s
This class follows Rails’ LogSubscriber pattern and provides common functionality
to provide structured logging capabilities for Rails components.
Base class for Sentry log subscribers that extends ActiveSupport::LogSubscriber
def detach_from(namespace, notifications = ActiveSupport::Notifications)
def detach_from(namespace, notifications = ActiveSupport::Notifications) listeners = public_instance_methods(false) .flat_map { |key| notifications.notifier.listeners_for("#{key}.#{namespace}") } .select { |listener| listener.instance_variable_get(:@delegate).is_a?(self) } listeners.map do |listener| notifications.notifier.unsubscribe(listener) end end
def duration_ms(event)
-
(Float)- Duration in milliseconds
Parameters:
-
event(ActiveSupport::Notifications::Event) -- The event
def duration_ms(event) event.duration.round(2) end
def log_structured_event(message:, level: :info, attributes: {}, origin: ORIGIN)
-
origin(String) -- The origin of the log event -
attributes(Hash) -- Additional structured attributes to include -
level(Symbol) -- The log level (:trace, :debug, :info, :warn, :error, :fatal) -
message(String) -- The log message
def log_structured_event(message:, level: :info, attributes: {}, origin: ORIGIN) Sentry.logger.public_send(level, message, **attributes, origin: origin) rescue => e # Silently handle any errors in logging to avoid breaking the application Sentry.configuration.sdk_logger.debug("Failed to log structured event: #{e.message}") end