Skip to content
This repository was archived by the owner on Mar 11, 2018. It is now read-only.

Custom fields #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,22 @@ In `config/production.rb`:
Scrolls::Rails.setup(Rails.application)
config.middleware.delete Rails::Rack::Logger
config.middleware.use Scrolls::Rails::Rack::QuietLogger

By default, your log output will look like this:

method=GET path=/ format=html controller=home action=show status=200 duration=23.210 view=17.190 db=0.610

You might want to add some custom fields to be logged, so in your controller
(or ApplicationController) you can add some custom keys to the payload hash,
like so:

def append_info_to_payload(payload)
super
payload[:ip] = request.remote_ip
payload[:user_id] = current_user.id.to_s if current_user
payload[:user_email] = current_user.email if current_user
end

And then configure `scroll-rails` to log those fields:

Scrolls::Rails.custom_fields = [:ip, :user_id, :user_email]
8 changes: 8 additions & 0 deletions lib/scrolls/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ def detach_existing_subscribers
end
end

def custom_fields=(fields)
@custom_fields = fields
end

def custom_fields
@custom_fields || []
end

# private

def unsubscribe(component, subscriber)
Expand Down
11 changes: 6 additions & 5 deletions lib/scrolls/rails/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ module Scrolls
module Rails
class LogSubscriber < ActiveSupport::LogSubscriber

FIELDS = [
:method, :path, :format, :controller, :action, :status, :error,
:duration, :view, :db, :location
]

def process_action(event)
exception = event.payload[:exception]
if exception
Expand All @@ -31,6 +26,7 @@ def extract_request_data_from_event(event)
data[:status] = extract_status(event.payload)
data.merge! runtimes(event)
data.merge! location(event)
data.merge! custom_fields(event)
end

def extract_request(payload)
Expand All @@ -51,6 +47,7 @@ def extract_status(payload)
end
end


def runtimes(event)
{ :duration => event.duration,
:view => event.payload[:view_runtime],
Expand All @@ -69,6 +66,10 @@ def location(event)
{}
end
end

def custom_fields(event)
event.payload.slice(*Scrolls::Rails.custom_fields)
end
end
end
end