Skip to content

Commit

Permalink
Extracted logging code from Travis.
Browse files Browse the repository at this point in the history
Not fully tested yet.
  • Loading branch information
roidrage committed Mar 10, 2012
1 parent c91cdd3 commit ab4a7c0
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
9 changes: 9 additions & 0 deletions Guardfile
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

19 changes: 19 additions & 0 deletions lib/lograge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@

module Lograge
mattr_accessor :logger

def self.remove_existing_log_subscriptions
%w(redirect_to process_action start_processing send_data write_fragment exist_fragment? send_file).each do |event|
ActiveSupport::Notifications.notifier.listeners_for("#{event}.action_controller").each do |listener|
if listener.inspect =~ /delegate[^a-z]+ActionController/
ActiveSupport::Notifications.unsubscribe listener
end
end

end

%w{render_template render_partial render_collection}.each do |event|
ActiveSupport::Notifications.notifier.listeners_for("#{event}.action_view").each do |listener|
if listener.inspect =~ /delegate[^a-z]+ActionView/
ActiveSupport::Notifications.unsubscribe listener
end
end
end
end
end

require 'lograge/railtie' if defined? Rails::Railtie
41 changes: 41 additions & 0 deletions lib/lograge/log_subscriber.rb
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
5 changes: 5 additions & 0 deletions lib/lograge/railtie.rb
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
2 changes: 2 additions & 0 deletions lograge.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,7 @@ Gem::Specification.new do |s|

# specify any dependencies here; for example:
s.add_development_dependency "rspec"
s.add_development_dependency "guard-rspec"
s.add_runtime_dependency "activesupport"
s.add_runtime_dependency "actionpack"
end
40 changes: 40 additions & 0 deletions spec/lograge_spec.rb
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
11 changes: 11 additions & 0 deletions spec/spec_helper.rb
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

0 comments on commit ab4a7c0

Please sign in to comment.