Skip to content

Commit

Permalink
Move Rails::LogSubscriber to ActiveSupport::LogSubscriber, allowing f…
Browse files Browse the repository at this point in the history
…rameworks like ActiveRecord and ActiveResource to log outsude Rails::Application [#4816 state:resolved]
  • Loading branch information
josevalim committed Jun 24, 2010
1 parent 5441e08 commit 6788db8
Show file tree
Hide file tree
Showing 34 changed files with 266 additions and 317 deletions.
1 change: 1 addition & 0 deletions actionmailer/lib/action_mailer/base.rb
Expand Up @@ -4,6 +4,7 @@
require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/proc'
require 'action_mailer/log_subscriber'

module ActionMailer #:nodoc:
# Action Mailer allows you to send email from your application using a mailer model and views.
Expand Down
22 changes: 22 additions & 0 deletions actionmailer/lib/action_mailer/log_subscriber.rb
@@ -0,0 +1,22 @@
require 'active_support/core_ext/array/wrap'

module ActionMailer
class LogSubscriber < ActiveSupport::LogSubscriber
def deliver(event)
recipients = Array.wrap(event.payload[:to]).join(', ')
info("\nSent mail to #{recipients} (%1.fms)" % event.duration)
debug(event.payload[:mail])
end

def receive(event)
info("\nReceived mail (%.1fms)" % event.duration)
debug(event.payload[:mail])
end

def logger
ActionMailer::Base.logger
end
end
end

ActionMailer::LogSubscriber.attach_to :action_mailer
3 changes: 0 additions & 3 deletions actionmailer/lib/action_mailer/railtie.rb
Expand Up @@ -5,9 +5,6 @@ module ActionMailer
class Railtie < Rails::Railtie
config.action_mailer = ActiveSupport::OrderedOptions.new

require "action_mailer/railties/log_subscriber"
log_subscriber :action_mailer, ActionMailer::Railties::LogSubscriber.new

initializer "action_mailer.logger" do
ActiveSupport.on_load(:action_mailer) { self.logger ||= Rails.logger }
end
Expand Down
22 changes: 0 additions & 22 deletions actionmailer/lib/action_mailer/railties/log_subscriber.rb

This file was deleted.

8 changes: 4 additions & 4 deletions actionmailer/test/log_subscriber_test.rb
@@ -1,13 +1,13 @@
require "abstract_unit"
require "rails/log_subscriber/test_helper"
require "action_mailer/railties/log_subscriber"
require "active_support/log_subscriber/test_helper"
require "action_mailer/log_subscriber"

class AMLogSubscriberTest < ActionMailer::TestCase
include Rails::LogSubscriber::TestHelper
include ActiveSupport::LogSubscriber::TestHelper

def setup
super
Rails::LogSubscriber.add(:action_mailer, ActionMailer::Railties::LogSubscriber.new)
ActionMailer::LogSubscriber.attach_to :action_mailer
end

class TestMailer < ActionMailer::Base
Expand Down
2 changes: 2 additions & 0 deletions actionpack/lib/action_controller/base.rb
@@ -1,3 +1,5 @@
require "action_controller/log_subscriber"

module ActionController
class Base < Metal
abstract!
Expand Down
56 changes: 56 additions & 0 deletions actionpack/lib/action_controller/log_subscriber.rb
@@ -0,0 +1,56 @@
require 'active_support/core_ext/object/blank'

module ActionController
class LogSubscriber < ActiveSupport::LogSubscriber
INTERNAL_PARAMS = %w(controller action format _method only_path)

def start_processing(event)
payload = event.payload
params = payload[:params].except(*INTERNAL_PARAMS)

info " Processing by #{payload[:controller]}##{payload[:action]} as #{payload[:formats].first.to_s.upcase}"
info " Parameters: #{params.inspect}" unless params.empty?
end

def process_action(event)
payload = event.payload
additions = ActionController::Base.log_process_action(payload)

message = "Completed #{payload[:status]} #{Rack::Utils::HTTP_STATUS_CODES[payload[:status]]} in %.0fms" % event.duration
message << " (#{additions.join(" | ")})" unless additions.blank?

info(message)
end

def send_file(event)
message = "Sent file %s"
message << " (%.1fms)"
info(message % [event.payload[:path], event.duration])
end

def redirect_to(event)
info "Redirected to #{event.payload[:location]}"
end

def send_data(event)
info("Sent data %s (%.1fms)" % [event.payload[:filename], event.duration])
end

%w(write_fragment read_fragment exist_fragment?
expire_fragment expire_page write_page).each do |method|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
def #{method}(event)
key_or_path = event.payload[:key] || event.payload[:path]
human_name = #{method.to_s.humanize.inspect}
info("\#{human_name} \#{key_or_path} (%.1fms)" % event.duration)
end
METHOD
end

def logger
ActionController::Base.logger
end
end
end

ActionController::LogSubscriber.attach_to :action_controller
3 changes: 0 additions & 3 deletions actionpack/lib/action_controller/railtie.rb
Expand Up @@ -6,7 +6,6 @@
require "active_support/deprecation/proxy_wrappers"
require "active_support/deprecation"

require "action_controller/railties/log_subscriber"
require "action_controller/railties/url_helpers"

module ActionController
Expand Down Expand Up @@ -35,8 +34,6 @@ class Railtie < Rails::Railtie
end
end

log_subscriber :action_controller, ActionController::Railties::LogSubscriber.new

initializer "action_controller.set_configs" do |app|
paths = app.config.paths
ac = app.config.action_controller
Expand Down
56 changes: 0 additions & 56 deletions actionpack/lib/action_controller/railties/log_subscriber.rb

This file was deleted.

1 change: 1 addition & 0 deletions actionpack/lib/action_view/base.rb
Expand Up @@ -3,6 +3,7 @@
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/array/wrap'
require 'active_support/ordered_options'
require 'action_view/log_subscriber'

module ActionView #:nodoc:
class NonConcattingString < ActiveSupport::SafeBuffer
Expand Down
27 changes: 27 additions & 0 deletions actionpack/lib/action_view/log_subscriber.rb
@@ -0,0 +1,27 @@
module ActionView
# = Action View Log Subscriber
#
# Provides functionality so that Rails can output logs from Action View.
class LogSubscriber < ActiveSupport::LogSubscriber
def render_template(event)
message = "Rendered #{from_rails_root(event.payload[:identifier])}"
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
message << (" (%.1fms)" % event.duration)
info(message)
end
alias :render_partial :render_template
alias :render_collection :render_template

def logger
ActionController::Base.logger
end

protected

def from_rails_root(string)
string.sub("#{Rails.root}/", "").sub(/^app\/views\//, "")
end
end
end

ActionView::LogSubscriber.attach_to :action_view
3 changes: 0 additions & 3 deletions actionpack/lib/action_view/railtie.rb
Expand Up @@ -8,9 +8,6 @@ class Railtie < Rails::Railtie
config.action_view.stylesheet_expansions = {}
config.action_view.javascript_expansions = { :defaults => ['prototype', 'effects', 'dragdrop', 'controls', 'rails'] }

require "action_view/railties/log_subscriber"
log_subscriber :action_view, ActionView::Railties::LogSubscriber.new

initializer "action_view.cache_asset_timestamps" do |app|
unless app.config.cache_classes
ActiveSupport.on_load(:action_view) do
Expand Down
27 changes: 0 additions & 27 deletions actionpack/lib/action_view/railties/log_subscriber.rb

This file was deleted.

10 changes: 5 additions & 5 deletions actionpack/test/activerecord/controller_runtime_test.rb
@@ -1,8 +1,8 @@
require 'active_record_unit'
require 'active_record/railties/controller_runtime'
require 'fixtures/project'
require 'rails/log_subscriber/test_helper'
require 'action_controller/railties/log_subscriber'
require 'active_support/log_subscriber/test_helper'
require 'action_controller/log_subscriber'

ActionController::Base.send :include, ActiveRecord::Railties::ControllerRuntime

Expand All @@ -13,18 +13,18 @@ def show
end
end

include Rails::LogSubscriber::TestHelper
include ActiveSupport::LogSubscriber::TestHelper
tests LogSubscriberController

def setup
super
@old_logger = ActionController::Base.logger
Rails::LogSubscriber.add(:action_controller, ActionController::Railties::LogSubscriber.new)
ActionController::LogSubscriber.attach_to :action_controller
end

def teardown
super
Rails::LogSubscriber.log_subscribers.clear
ActiveSupport::LogSubscriber.log_subscribers.clear
ActionController::Base.logger = @old_logger
end

Expand Down
10 changes: 5 additions & 5 deletions actionpack/test/controller/log_subscriber_test.rb
@@ -1,6 +1,6 @@
require "abstract_unit"
require "rails/log_subscriber/test_helper"
require "action_controller/railties/log_subscriber"
require "active_support/log_subscriber/test_helper"
require "action_controller/log_subscriber"

module Another
class LogSubscribersController < ActionController::Base
Expand Down Expand Up @@ -37,7 +37,7 @@ def with_page_cache

class ACLogSubscriberTest < ActionController::TestCase
tests Another::LogSubscribersController
include Rails::LogSubscriber::TestHelper
include ActiveSupport::LogSubscriber::TestHelper

def setup
super
Expand All @@ -47,12 +47,12 @@ def setup
@cache_path = File.expand_path('../temp/test_cache', File.dirname(__FILE__))
ActionController::Base.page_cache_directory = @cache_path
@controller.cache_store = :file_store, @cache_path
Rails::LogSubscriber.add(:action_controller, ActionController::Railties::LogSubscriber.new)
ActionController::LogSubscriber.attach_to :action_controller
end

def teardown
super
Rails::LogSubscriber.log_subscribers.clear
ActiveSupport::LogSubscriber.log_subscribers.clear
FileUtils.rm_rf(@cache_path)
ActionController::Base.logger = @old_logger
end
Expand Down
10 changes: 5 additions & 5 deletions actionpack/test/template/log_subscriber_test.rb
@@ -1,22 +1,22 @@
require "abstract_unit"
require "rails/log_subscriber/test_helper"
require "action_view/railties/log_subscriber"
require "active_support/log_subscriber/test_helper"
require "action_view/log_subscriber"
require "controller/fake_models"

class AVLogSubscriberTest < ActiveSupport::TestCase
include Rails::LogSubscriber::TestHelper
include ActiveSupport::LogSubscriber::TestHelper

def setup
super
@old_logger = ActionController::Base.logger
@view = ActionView::Base.new(ActionController::Base.view_paths, {})
Rails.stubs(:root).returns(File.expand_path(FIXTURE_LOAD_PATH))
Rails::LogSubscriber.add(:action_view, ActionView::Railties::LogSubscriber.new)
ActionView::LogSubscriber.attach_to :action_view
end

def teardown
super
Rails::LogSubscriber.log_subscribers.clear
ActiveSupport::LogSubscriber.log_subscribers.clear
ActionController::Base.logger = @old_logger
end

Expand Down
1 change: 1 addition & 0 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -20,6 +20,7 @@
require 'active_support/core_ext/object/blank'
require 'arel'
require 'active_record/errors'
require 'active_record/log_subscriber'

module ActiveRecord #:nodoc:
# = Active Record
Expand Down

1 comment on commit 6788db8

@norman
Copy link
Contributor

@norman norman commented on 6788db8 Jun 24, 2010

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very glad to see this change!

Please sign in to comment.