Class: Scrolls::Rails::LogSubscriber

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

Constant Summary collapse

FIELDS =
[
  :method, :path, :format, :controller, :action, :status, :error,
  :duration, :view, :db, :location
]

Instance Method Summary collapse

Instance Method Details

#extract_request(payload) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/scrolls/rails/log_subscriber.rb', line 36

def extract_request(payload)
  {
    :method => payload[:method],
    :path => payload[:path],
    :format => payload[:format],
    :controller => payload[:params]['controller'],
    :action => payload[:params]['action']
  }
end

#extract_request_data_from_event(event) ⇒ Object

private



29
30
31
32
33
34
# File 'lib/scrolls/rails/log_subscriber.rb', line 29

def extract_request_data_from_event(event)
  data = extract_request(event.payload)
  data[:status] = extract_status(event.payload)
  data.merge! runtimes(event)
  data.merge! location(event)
end

#extract_status(payload) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/scrolls/rails/log_subscriber.rb', line 46

def extract_status(payload)
  if payload[:status]
    payload[:status].to_i
  else
    0
  end
end

#location(event) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/scrolls/rails/log_subscriber.rb', line 64

def location(event)
  if location = Thread.current[:scrolls_rails_location]
    Thread.current[:scrolls_rails_location] = nil
    { :location => location }
  else
    {}
  end
end

#process_action(event) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/scrolls/rails/log_subscriber.rb', line 14

def process_action(event)
  exception = event.payload[:exception]
  if exception
    Scrolls.log_exception({status: 500}, exception)
  else
    Scrolls.log(extract_request_data_from_event(event))
  end
end

#redirect_to(event) ⇒ Object



23
24
25
# File 'lib/scrolls/rails/log_subscriber.rb', line 23

def redirect_to(event)
  Thread.current[:scrolls_rails_location] = event.payload[:location]
end

#runtimes(event) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/scrolls/rails/log_subscriber.rb', line 54

def runtimes(event)
  { :duration => event.duration,
    :view => event.payload[:view_runtime],
    :db => event.payload[:db_runtime]
  }.inject({}) do |runtimes, (name, runtime)|
    runtimes[name] = runtime.to_f.round(2) if runtime
    runtimes
  end
end