Class: Logtail::Integrations::Rails::EventLogSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/logtail-rails/event_log_subscriber.rb

Overview

A subscriber for Rails 8.1’s Structured Event Reporting system. This subscriber receives events emitted by Rails.event.notify() and logs them with all their data to Rails.logger, which sends them to Better Stack.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger) ⇒ EventLogSubscriber

Initialize the subscriber with a logger instance



18
19
20
# File 'lib/logtail-rails/event_log_subscriber.rb', line 18

def initialize(logger)
  @logger = logger
end

Instance Attribute Details

#loggerObject (readonly)

Rails logger instance



9
10
11
# File 'lib/logtail-rails/event_log_subscriber.rb', line 9

def logger
  @logger
end

Instance Method Details

#emit(event) ⇒ Object

Rails 8.1 event emission method - called when events are emitted



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/logtail-rails/event_log_subscriber.rb', line 23

def emit(event)
  return unless self.class.enabled

  # Log the event with all its data to Rails.logger
  # Create a structured log entry with the event data
  tags = event[:tags]
  tags_array = tags.is_a?(Hash) ? tags.keys : (tags.is_a?(Array) ? tags : [])

  log_data = {
    event_name: event[:name],
    payload: event[:payload] || {},
    context: event[:context] || {},
    tags: tags_array,
    source_location: event[:source_location] || {}
  }

  message = build_log_message(event)
  logger.send(self.class.log_level, message, log_data)
end