Permalink
Browse files

Added support for sending messages to Graylog2

uses the GELF format and requires at least version 1.4.0 of the gelf gem as logger for proper formatting.
  • Loading branch information...
1 parent 1930fb7 commit 44772b0fbe03f21c6bf8c9e3b5d2ba44c9cdb97a @lennartkoopmann lennartkoopmann committed Jan 26, 2013
Showing with 116 additions and 1 deletion.
  1. +14 −0 lib/lograge/log_subscriber.rb
  2. +1 −1 lib/lograge/version.rb
  3. +101 −0 spec/lograge_logsubscriber_spec.rb
@@ -43,6 +43,20 @@ def process_action_logstash(data)
event.to_json
end
+ 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
+
def redirect_to(event)
Thread.current[:lograge_location] = event.payload[:location]
end
View
@@ -1,3 +1,3 @@
module Lograge
- VERSION = "0.1.2"
+ VERSION = "0.2.0"
end
@@ -196,6 +196,107 @@
end
end
+ describe "when processing an action with graylog2 output" do
+ before do
+ Lograge::log_format = :graylog2
+ end
+
+ it "should include the URL in the log output" do
+ subscriber.process_action(event)
+ log_output.string.should include(':_path=>"/home"')
+ end
+
+ it "should start include the HTTP method" do
+ subscriber.process_action(event)
+ log_output.string.should include(':_method=>"GET"')
+ end
+
+ it "should include the status code" do
+ subscriber.process_action(event)
+ log_output.string.should include(':_status=>200') end
+
+ it "should include the controller and action" do
+ subscriber.process_action(event)
+ log_output.string.should include(':_controller=>"home"')
+ log_output.string.should include(':_action=>"index"')
+ end
+
+ it "should include the duration" do
+ subscriber.process_action(event)
+ log_output.string.should =~ /:_duration=>\d+\.\d{0,2}/
+ end
+
+ it "should include the view rendering time" do
+ subscriber.process_action(event)
+ log_output.string.should include(':_view=>0.01')
+ end
+
+ it "should include the database rendering time" do
+ subscriber.process_action(event)
+ log_output.string.should include(':_db=>0.02')
+ end
+
+ it "should add a 500 status when an exception occurred" do
+ event.payload[:status] = nil
+ event.payload[:exception] = ['AbstractController::ActionNotFound', 'Route not found']
+ subscriber.process_action(event)
+ log_output.string.should include(':_status=>500')
+ log_output.string.should include(':_error=>"AbstractController::ActionNotFound:Route not found"')
+ end
+
+ it "should return an unknown status when no status or exception is found" do
+ event.payload[:status] = nil
+ event.payload[:exception] = nil
+ subscriber.process_action(event)
+ log_output.string.should include(':_status=>0')
+ end
+
+ describe "with a redirect" do
+ before do
+ Thread.current[:lograge_location] = "http://www.example.com"
+ end
+
+ it "should add the location to the log line" do
+ subscriber.process_action(event)
+ log_output.string.should include(':_location=>"http://www.example.com"')
+ end
+
+ it "should remove the thread local variable" do
+ subscriber.process_action(event)
+ Thread.current[:lograge_location].should == nil
+ end
+ end
+
+ it "should not include a location by default" do
+ subscriber.process_action(event)
+ log_output.string.should_not =~ /"location":/
+ end
+ end
+
+ describe "with custom_options configured for graylog2 output" do
+ before do
+ Lograge::log_format = :graylog2
+ end
+
+ it "should combine the hash properly for the output" do
+ Lograge.custom_options = {:data => "value"}
+ subscriber.process_action(event)
+ log_output.string.should include(':_data=>"value"')
+ end
+
+ it "should combine the output of a lambda properly" do
+ Lograge.custom_options = lambda {|event| {:data => "value"}}
+ subscriber.process_action(event)
+ log_output.string.should include(':_data=>"value"')
+ end
+
+ it "should work if the method returns nil" do
+ Lograge.custom_options = lambda {|event| nil}
+ subscriber.process_action(event)
+ log_output.string.should be_present
+ end
+ end
+
describe "with custom_options configured for lograge output" do
before do
Lograge::log_format = :lograge

0 comments on commit 44772b0

Please sign in to comment.