Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

adding an extra hook to allow custom logging in a simple format

  • Loading branch information...
commit abe2ed7ef52dd256d6d362cc26fdca223bcfa32b 1 parent 38d255e
@adamcooper adamcooper authored
View
17 README.md
@@ -55,7 +55,20 @@ Enable it for the relevant environments, e.g. production:
```
# config/environments/production.rb
MyApp::Application.configure do
- config.lograge.enabled = true
+ config.lograge.enabled = true
+end
+```
+
+You can also add a hook for own custom data
+
+```
+# config/environments/staging.rb
+MyApp::Application.configure do
+ config.lograge.enabled = true
+ config.lograge.extra = lambda do |event|
+ # some custom details
+ " custom data like GC stats"
+ end
end
```
@@ -90,7 +103,7 @@ Started GET "/" for 127.0.0.1 at 2012-03-12 17:10:10 +0100
And the verbose output coming from rack-cache:
```
-cache: [GET /] miss
+cache: [GET /] miss
```
Both are independent of the LogSubscribers, and both need to be shut up using
View
9 lib/lograge.rb
@@ -5,7 +5,13 @@
require 'active_support/ordered_options'
module Lograge
- mattr_accessor :logger
+ mattr_accessor :logger
+
+ mattr_accessor :extra
+
+ def self.extra_info(event)
+ extra.call(event) if self.extra
+ end
def self.remove_existing_log_subscriptions
%w(redirect_to process_action start_processing send_data write_fragment exist_fragment? send_file).each do |event|
@@ -31,6 +37,7 @@ def self.setup(app)
require 'lograge/rails_ext/rack/logger'
Lograge.remove_existing_log_subscriptions
Lograge::RequestLogSubscriber.attach_to :action_controller
+ Lograge.extra = app.config.lograge.extra
end
end
View
7 lib/lograge/log_subscriber.rb
@@ -9,6 +9,7 @@ def process_action(event)
message << extract_status(payload)
message << runtimes(event)
message << location(event)
+ message << extra_info(event)
logger.info(message)
end
@@ -27,6 +28,10 @@ def extract_status(payload)
end
end
+ def extra_info(event)
+ Lograge.extra_info(event).to_s
+ end
+
def runtimes(event)
message = ""
{:duration => event.duration,
@@ -40,7 +45,7 @@ def runtimes(event)
def location(event)
if location = Thread.current[:lograge_location]
Thread.current[:lograge_location] = nil
- "location=#{location}"
+ " location=#{location}"
else
""
end
View
18 spec/lograge_logsubscriber_spec.rb
@@ -1,4 +1,5 @@
require 'spec_helper'
+require 'lograge'
require 'lograge/log_subscriber'
require 'active_support/notifications'
require 'active_support/core_ext/string'
@@ -85,7 +86,7 @@
it "should add the location to the log line" do
subscriber.process_action(event)
- log_output.string.should =~ %r{location=http://www.example.com}
+ log_output.string.should =~ %r{ location=http://www.example.com}
end
it "should remove the thread local variable" do
@@ -93,13 +94,26 @@
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 an extra_logger configured" do
+ it "should add the extra logger output to the logfile" do
+ Lograge.extra = lambda {|event| "extra_data"}
+ subscriber.process_action(event)
+ log_output.string.should =~ /extra_data/
+ end
+ it "should work if the method returns nil" do
+ Lograge.extra = lambda {|event| nil}
+ subscriber.process_action(event)
+ log_output.string.should be_present
+ end
+ end
+
describe "when processing a redirect" do
it "should store the location in a thread local variable" do
subscriber.redirect_to(redirect)
Please sign in to comment.
Something went wrong with that request. Please try again.