lib/logstasher/log_subscriber.rb in logstasher-0.1.1 vs lib/logstasher/log_subscriber.rb in logstasher-0.2.0
- old
+ new
@@ -1,22 +1,23 @@
require 'active_support/core_ext/class/attribute'
require 'active_support/log_subscriber'
-module Logstasher
+module LogStasher
class RequestLogSubscriber < ActiveSupport::LogSubscriber
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! extract_exception(payload)
- data.merge! extract_appended_params(payload)
+ data.merge! extract_custom_fields(payload)
- event = LogStash::Event.new("@fields" => data)
- Logstasher.logger.info event.to_json
+ event = LogStash::Event.new('@fields' => data, '@tags' => ['request'])
+ event.tags << 'exception' if payload[:exception]
+ LogStasher.logger.unknown event.to_json
end
def redirect_to(event)
Thread.current[:logstasher_location] = event.payload[:location]
end
@@ -26,12 +27,12 @@
def extract_request(payload)
{
:method => payload[:method],
:path => extract_path(payload),
:format => extract_format(payload),
- :controller => payload[:controller],
- :action => payload[:action]
+ :controller => payload[:params]['controller'],
+ :action => payload[:params]['action']
}
end
def extract_path(payload)
payload[:path].split("?").first
@@ -47,11 +48,11 @@
def extract_status(payload)
if payload[:status]
{ :status => payload[:status].to_i }
else
- {}
+ { :status => 0 }
end
end
def runtimes(event)
{
@@ -75,18 +76,19 @@
# Monkey patching to enable exception logging
def extract_exception(payload)
if payload[:exception]
exception, message = payload[:exception]
- message = "#{exception} : #{message}\n #{($!.backtrace.join("\n"))}"
+ message = "#{exception}\n#{message}\n#{($!.backtrace.join("\n"))}"
{ :status => 500, :error => message }
else
{}
end
end
- def extract_appended_params(payload)
- appended_keys = payload.delete(:log_stasher_appended_param_keys)
- (appended_keys && payload.extract!(*appended_keys)) || {}
+ def extract_custom_fields(payload)
+ custom_fields = (!LogStasher.custom_fields.empty? && payload.extract!(*LogStasher.custom_fields)) || {}
+ LogStasher.custom_fields.clear
+ custom_fields
end
end
end