class LogStasher::LogSubscriber

Constants

INTERNAL_PARAMS

Public Instance Methods

process_action(event) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 20
def process_action(event)
  payload = event.payload
  tags    = extract_tags(payload)
  fields  = extract_request(payload)

  fields.merge! extract_status(payload)
  fields.merge! runtimes(event)
  fields.merge! location
  fields.merge! extract_exception(payload)
  fields.merge! extract_parameters(payload)
  fields.merge! appended_fields

  event = LogStash::Event.new(fields.merge('tags' => tags))

  LogStasher.logger << event.to_json + "\n"
end
redirect_to(event) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 37
def redirect_to(event)
  Thread.current[:logstasher_context][:location] = event.payload[:location]
end

Private Instance Methods

appended_fields() click to toggle source
# File lib/logstasher/log_subscriber.rb, line 43
def appended_fields
  callback = ::LogStasher.append_fields_callback
  {}.tap do |fields|
    controller.instance_exec(fields, &callback) if callback
  end
end
controller() click to toggle source
# File lib/logstasher/log_subscriber.rb, line 50
def controller
  Thread.current[:logstasher_context][:controller]
end
extract_exception(payload) click to toggle source

Monkey patching to enable exception logging

# File lib/logstasher/log_subscriber.rb, line 68
def extract_exception(payload)
  if payload[:exception]
    exception, message = payload[:exception]
    status = ActionDispatch::ExceptionWrapper.status_code_for_exception(exception)
    message = "#{exception}\n#{message}\n#{($!.backtrace.join("\n"))}"
    { :status => status, :error => message }
  else
    {}
  end
end
extract_format(payload) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 79
def extract_format(payload)
  if ::ActionPack::VERSION::MAJOR == 3 && ::ActionPack::VERSION::MINOR == 0
    payload[:formats].first
  else
    payload[:format]
  end
end
extract_parameters(payload) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 87
def extract_parameters(payload)
  if LogStasher.include_parameters?
    external_params = payload[:params].except(*INTERNAL_PARAMS)

    if LogStasher.serialize_parameters?
      { :params => JSON.generate(external_params) }
    else
      { :params => external_params }
    end
  else
    {}
  end
end
extract_path(payload) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 101
def extract_path(payload)
  payload[:path].split("?").first
end
extract_request(payload) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 54
def extract_request(payload)
  {
    :action     => payload[:action],
    :controller => payload[:controller],
    :format     => extract_format(payload),
    :ip         => request.remote_ip,
    :request_id => request.env["action_dispatch.request_id"],
    :method     => payload[:method],
    :path       => extract_path(payload),
    :route      => "#{payload[:controller]}##{payload[:action]}"
  }
end
extract_status(payload) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 105
def extract_status(payload)
  if payload[:status]
    { :status => payload[:status].to_i }
  else
    { :status => 0 }
  end
end
extract_tags(payload) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 113
def extract_tags(payload)
  tags = ['request']
  tags.push('exception') if payload[:exception]
  tags
end
location() click to toggle source
# File lib/logstasher/log_subscriber.rb, line 119
def location
  location = Thread.current[:logstasher_context][:location]

  if location
    { :location => location }
  else
    {}
  end
end
request() click to toggle source
# File lib/logstasher/log_subscriber.rb, line 129
def request
  Thread.current[:logstasher_context][:request]
end
runtimes(event) click to toggle source
# File lib/logstasher/log_subscriber.rb, line 133
def runtimes(event)
  {
    :total => event.duration,
    :view  => event.payload[:view_runtime],
    :db    => event.payload[:db_runtime]
  }.inject({:runtime => {}}) do |runtimes, (name, runtime)|
    runtimes[:runtime][name] = runtime.to_f.round(2) if runtime
    runtimes
  end
end