Class: BrainzLab::Rails::LogSubscriber

Inherits:
ActiveSupport::LogSubscriber
  • Object
show all
Defined in:
lib/brainzlab/rails/log_subscriber.rb

Constant Summary collapse

INTERNAL_PARAMS =
%w[controller action format _method authenticity_token].freeze

Class Attribute Summary collapse

Instance Method Summary collapse

Class Attribute Details

.formatterObject

Returns the value of attribute formatter.



11
12
13
# File 'lib/brainzlab/rails/log_subscriber.rb', line 11

def formatter
  @formatter
end

Instance Method Details

#halted_callback(event) ⇒ Object



55
56
57
# File 'lib/brainzlab/rails/log_subscriber.rb', line 55

def halted_callback(event)
  # Request was halted by a before_action
end

#process_action(event) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/brainzlab/rails/log_subscriber.rb', line 31

def process_action(event)
  return unless formatter

  request_id = event.payload[:request]&.request_id || Thread.current[:brainzlab_request_id]
  return unless request_id

  payload = event.payload

  formatter.process_action(request_id,
                           controller: payload[:controller],
                           action: payload[:action],
                           status: payload[:status],
                           duration: event.duration,
                           view_runtime: payload[:view_runtime],
                           db_runtime: payload[:db_runtime])

  # Handle exception if present
  formatter.error(request_id, payload[:exception_object]) if payload[:exception_object]

  # Output the formatted log
  output = formatter.end_request(request_id)
  log_output(output) if output
end

#redirect_to(event) ⇒ Object



59
60
61
# File 'lib/brainzlab/rails/log_subscriber.rb', line 59

def redirect_to(event)
  # Redirect happened
end

#start_processing(event) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/brainzlab/rails/log_subscriber.rb', line 14

def start_processing(event)
  return unless formatter

  request_id = event.payload[:request]&.request_id || Thread.current[:brainzlab_request_id]
  return unless request_id

  payload = event.payload
  params = payload[:params]&.except(*INTERNAL_PARAMS) || {}

  formatter.start_request(request_id,
                          method: payload[:method],
                          path: payload[:path],
                          params: filter_params(params),
                          controller: payload[:controller],
                          action: payload[:action])
end