Class: RailsLokiExporter::CustomLogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/rails_loki_exporter/custom_log_subscriber.rb

Constant Summary collapse

INTERNAL_PARAMS =
%w(controller action format _method only_path)

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.clientObject

Returns the value of attribute client.



8
9
10
# File 'lib/rails_loki_exporter/custom_log_subscriber.rb', line 8

def client
  @client
end

Instance Method Details

#process_action(event) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/rails_loki_exporter/custom_log_subscriber.rb', line 11

def process_action(event)
  if self.class.client
    
    payload = event.payload
    controller = payload[:controller]
    action = payload[:action]
    status = payload[:status]
    duration = event.duration.round(2)
    path = payload[:path]
    params = payload[:params].except(*INTERNAL_PARAMS)
    query_string = params.to_query

    request_url = query_string.blank? ? path : "#{path}?#{query_string}"
    log_message = "LogSubscriber - [#{controller}##{action}] URL:#{request_url}, Params: #{params}, Status: #{status}, Duration: #{duration}ms"
    self.class.client.send_log(log_message)
  end
end