-
-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Not fully tested yet.
- Loading branch information
Showing
8 changed files
with
129 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
--color | ||
--format progress |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# A sample Guardfile | ||
# More info at https://github.com/guard/guard#readme | ||
|
||
guard 'rspec', :version => 2 do | ||
watch(%r{^spec/.+_spec\.rb$}) | ||
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" } | ||
watch('spec/spec_helper.rb') { "spec" } | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
require 'active_support/core_ext/class/attribute' | ||
require 'active_support/log_subscriber' | ||
|
||
module Lograge | ||
class LogSubscriber < ActiveSupport::LogSubscriber | ||
class_attribute :logger | ||
|
||
def initialize | ||
self.logger ||= ActionController::Base.logger | ||
end | ||
|
||
def process_action(event) | ||
payload = event.payload | ||
message = "#{payload[:method]} #{payload[:path]} format=#{payload[:format]} action=#{payload[:params]['controller']}##{payload[:params]['action']}" | ||
message << extract_status(payload) | ||
message << runtimes(event) | ||
logger.info(message) | ||
end | ||
|
||
private | ||
|
||
def extract_status(payload) | ||
if payload[:status] | ||
" status=#{payload[:status]}" | ||
elsif payload[:exception] | ||
exception, message = payload[:exception] | ||
" status=500 error='#{exception}:#{message}'" | ||
end | ||
end | ||
|
||
def runtimes(event) | ||
message = "" | ||
{:duration => event.duration, | ||
:view => event.payload[:view_runtime], | ||
:db => event.payload[:db_runtime]}.each do |name, runtime| | ||
message << " #{name}=%.2f" % runtime if runtime | ||
end | ||
message | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,11 @@ | ||
require 'rails/railtie' | ||
require 'lograge/log_subscriber' | ||
|
||
module Lograge | ||
class Railtie < Rails::Railtie | ||
initializer :lograge do |app| | ||
Lograge.remove_existing_log_subscriptions | ||
Lograge::LogSubscriber.attach_to | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
require 'spec_helper' | ||
require 'lograge' | ||
require 'active_support/notifications' | ||
require 'active_support/core_ext/string' | ||
require 'active_support/log_subscriber' | ||
require 'action_controller/log_subscriber' | ||
require 'action_view/log_subscriber' | ||
|
||
describe Lograge do | ||
describe "when removing Rails' log subscribers" do | ||
after do | ||
ActionController::LogSubscriber.attach_to :action_controller | ||
ActionView::LogSubscriber.attach_to :action_view | ||
end | ||
|
||
it "should remove subscribers for controller events" do | ||
expect { | ||
Lograge.remove_existing_log_subscriptions | ||
}.to change { | ||
ActiveSupport::Notifications.notifier.listeners_for('process_action.action_controller') | ||
} | ||
end | ||
|
||
it "should remove subscribers for all events" do | ||
expect { | ||
Lograge.remove_existing_log_subscriptions | ||
}.to change { | ||
ActiveSupport::Notifications.notifier.listeners_for('render_template.action_view') | ||
} | ||
end | ||
|
||
it "shouldn't remove subscribers that aren't from Rails" do | ||
blk = -> {} | ||
ActiveSupport::Notifications.subscribe("process_action.action_controller", &blk) | ||
Lograge.remove_existing_log_subscriptions | ||
listeners = ActiveSupport::Notifications.notifier.listeners_for('process_action.action_controller') | ||
listeners.size.should > 0 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
# This file was generated by the `rspec --init` command. Conventionally, all | ||
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. | ||
# Require this file using `require "spec_helper.rb"` to ensure that it is only | ||
# loaded once. | ||
# | ||
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration | ||
RSpec.configure do |config| | ||
config.treat_symbols_as_metadata_keys_with_true_values = true | ||
config.run_all_when_everything_filtered = true | ||
config.filter_run :focus | ||
end |