Skip to content

Commit

Permalink
Merge pull request #59 from i0rek/before_format
Browse files Browse the repository at this point in the history
Add before_format hook.
  • Loading branch information
roidrage committed Mar 11, 2014
2 parents 3747f3e + 5b52fbe commit c2e5868
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
13 changes: 13 additions & 0 deletions lib/lograge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def self.custom_options(event)
end
end

# Before format allows you to change the structure of the output.
# You've to pass in something callable
#
mattr_writer :before_format
self.before_format = nil

def self.before_format(data, payload)
result = nil
result = @@before_format.call(data, payload) if @@before_format
result || data
end

# Set conditions for events that should be ignored
#
# Currently supported formats are:
Expand Down Expand Up @@ -106,6 +118,7 @@ def self.setup(app)
Lograge.remove_existing_log_subscriptions
Lograge::RequestLogSubscriber.attach_to :action_controller
Lograge.custom_options = app.config.lograge.custom_options
Lograge.before_format = app.config.lograge.before_format
Lograge.log_level = app.config.lograge.log_level || :info
self.support_deprecated_config(app) # TODO: Remove with version 1.0
Lograge.formatter = app.config.lograge.formatter || Lograge::Formatters::KeyValue.new
Expand Down
7 changes: 6 additions & 1 deletion lib/lograge/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Lograge
class RequestLogSubscriber < ActiveSupport::LogSubscriber
def process_action(event)
return if Lograge.ignore?(event)

payload = event.payload

data = extract_request(payload)
Expand All @@ -16,6 +16,7 @@ def process_action(event)
data.merge! location(event)
data.merge! custom_options(event)

data = before_format(data, payload)
formatted_message = Lograge.formatter.call(data)
logger.send(Lograge.log_level, formatted_message)
end
Expand Down Expand Up @@ -67,6 +68,10 @@ def custom_options(event)
Lograge.custom_options(event) || {}
end

def before_format(data, payload)
Lograge.before_format(data, payload)
end

def runtimes(event)
{
:duration => event.duration,
Expand Down
21 changes: 21 additions & 0 deletions spec/lograge_logsubscriber_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,27 @@
end
end

describe "with before_format configured for lograge output" do
before do
Lograge.formatter = Lograge::Formatters::KeyValue.new
Lograge.before_format = nil
end

it "should output correctly" do
Lograge.before_format = lambda { |data, payload|
Hash[*data.first].merge(Hash[*payload.first])
}
subscriber.process_action(event)
log_output.string.should include("method=GET")
log_output.string.should include("status=200")
end
it "should work if the method returns nil" do
Lograge.before_format = lambda {|data, payload| nil}
subscriber.process_action(event)
log_output.string.should be_present
end
end

describe "with ignore configured" do
before do
# Lograge::log_format = :lograge
Expand Down

0 comments on commit c2e5868

Please sign in to comment.