Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Extracted logging code from Travis.

Not fully tested yet.
  • Loading branch information...
commit ab4a7c04dfb6fcaeb46884f2aee7a7477ea914c2 1 parent c91cdd3
@roidrage authored
View
2  .rspec
@@ -0,0 +1,2 @@
+--color
+--format progress
View
9 Guardfile
@@ -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
+
View
19 lib/lograge.rb
@@ -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
View
41 lib/lograge/log_subscriber.rb
@@ -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
View
5 lib/lograge/railtie.rb
@@ -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
View
2  lograge.gemspec
@@ -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
View
40 spec/lograge_spec.rb
@@ -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
View
11 spec/spec_helper.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.