Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add before_format hook. #59

Merged
merged 1 commit into from
Mar 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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