Class: Lograge::RequestLogSubscriber

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

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#process_action(event) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/lograge/log_subscriber.rb', line 6

def process_action(event)
  payload = event.payload

  data      = extract_request(payload)
  data.merge! extract_status(payload)
  data.merge! runtimes(event)
  data.merge! location(event)
  data.merge! custom_options(event)

  logger.info send(:"process_action_#{Lograge.log_format}", data)
end

#process_action_graylog2(data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/lograge/log_subscriber.rb', line 46

def process_action_graylog2(data)
  # Cloning because we don't want to mess with the original when mutating keys.
  my = data.clone

  base = {
    :short_message => "[#{my[:status]}] #{my[:method]} #{my[:path]} (#{my[:controller]}##{my[:action]})"
  }

  # Add underscore to every key to follow GELF additional field syntax.
  my.keys.each { |k| my["_#{k}".to_sym] = my[k]; my.delete(k) }

  my.merge(base)
end

#process_action_lograge(data) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/lograge/log_subscriber.rb', line 22

def process_action_lograge(data)
  fields  = LOGRAGE_FIELDS
  fields += (data.keys - LOGRAGE_FIELDS)

  event = fields.inject([]) do |message, key|
    next message unless data.has_key?(key)
    # Exactly preserve the previous output
    # Parsing this can be ambigious if the error messages contains
    # a single quote
    data[key] = "'#{data[key]}'" if key == :error
    # Ensure that we always have exactly two decimals
    data[key] = "%.2f" % data[key] if data[key].is_a? Float

    message << "#{key}=#{data[key]}"
    message
  end
  event.join(" ")
end

#process_action_logstash(data) ⇒ Object



41
42
43
44
# File 'lib/lograge/log_subscriber.rb', line 41

def process_action_logstash(data)
  event = LogStash::Event.new("@fields" => data)
  event.to_json
end

#redirect_to(event) ⇒ Object



60
61
62
# File 'lib/lograge/log_subscriber.rb', line 60

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