mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Move Rails::LogSubscriber to ActiveSupport::LogSubscriber, allowing frameworks like ActiveRecord and ActiveResource to log outsude Rails::Application [#4816 state:resolved]
This commit is contained in:
parent
5441e082f9
commit
6788db824a
34 changed files with 266 additions and 317 deletions
|
@ -4,6 +4,7 @@ require 'action_mailer/collector'
|
|||
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.
|
||||
|
|
22
actionmailer/lib/action_mailer/log_subscriber.rb
Normal file
22
actionmailer/lib/action_mailer/log_subscriber.rb
Normal file
|
@ -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
|
|
@ -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
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
require 'active_support/core_ext/array/wrap'
|
||||
|
||||
module ActionMailer
|
||||
module Railties
|
||||
class LogSubscriber < Rails::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
|
||||
end
|
|
@ -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
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require "action_controller/log_subscriber"
|
||||
|
||||
module ActionController
|
||||
class Base < Metal
|
||||
abstract!
|
||||
|
|
56
actionpack/lib/action_controller/log_subscriber.rb
Normal file
56
actionpack/lib/action_controller/log_subscriber.rb
Normal file
|
@ -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
|
|
@ -6,7 +6,6 @@ require "active_support/core_ext/class/subclasses"
|
|||
require "active_support/deprecation/proxy_wrappers"
|
||||
require "active_support/deprecation"
|
||||
|
||||
require "action_controller/railties/log_subscriber"
|
||||
require "action_controller/railties/url_helpers"
|
||||
|
||||
module ActionController
|
||||
|
@ -35,8 +34,6 @@ module ActionController
|
|||
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
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
require 'active_support/core_ext/object/blank'
|
||||
|
||||
module ActionController
|
||||
module Railties
|
||||
class LogSubscriber < Rails::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
|
||||
end
|
|
@ -3,6 +3,7 @@ require 'active_support/core_ext/module/delegation'
|
|||
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
|
||||
|
|
27
actionpack/lib/action_view/log_subscriber.rb
Normal file
27
actionpack/lib/action_view/log_subscriber.rb
Normal file
|
@ -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
|
|
@ -8,9 +8,6 @@ module ActionView
|
|||
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
|
||||
|
|
|
@ -1,27 +0,0 @@
|
|||
module ActionView
|
||||
# = Action View Log Subscriber
|
||||
#
|
||||
# Provides functionality so that Rails can output logs from Action View.
|
||||
module Railties
|
||||
class LogSubscriber < Rails::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
|
||||
end
|
|
@ -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
|
||||
|
||||
|
@ -13,18 +13,18 @@ class ControllerRuntimeLogSubscriberTest < ActionController::TestCase
|
|||
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
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -37,7 +37,7 @@ end
|
|||
|
||||
class ACLogSubscriberTest < ActionController::TestCase
|
||||
tests Another::LogSubscribersController
|
||||
include Rails::LogSubscriber::TestHelper
|
||||
include ActiveSupport::LogSubscriber::TestHelper
|
||||
|
||||
def setup
|
||||
super
|
||||
|
@ -47,12 +47,12 @@ class ACLogSubscriberTest < ActionController::TestCase
|
|||
@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
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ require 'active_support/core_ext/object/duplicable'
|
|||
require 'active_support/core_ext/object/blank'
|
||||
require 'arel'
|
||||
require 'active_record/errors'
|
||||
require 'active_record/log_subscriber'
|
||||
|
||||
module ActiveRecord #:nodoc:
|
||||
# = Active Record
|
||||
|
|
32
activerecord/lib/active_record/log_subscriber.rb
Normal file
32
activerecord/lib/active_record/log_subscriber.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
module ActiveRecord
|
||||
class LogSubscriber < ActiveSupport::LogSubscriber
|
||||
def initialize
|
||||
super
|
||||
@odd_or_even = false
|
||||
end
|
||||
|
||||
def sql(event)
|
||||
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
|
||||
sql = event.payload[:sql].squeeze(' ')
|
||||
|
||||
if odd?
|
||||
name = color(name, :cyan, true)
|
||||
sql = color(sql, nil, true)
|
||||
else
|
||||
name = color(name, :magenta, true)
|
||||
end
|
||||
|
||||
debug " #{name} #{sql}"
|
||||
end
|
||||
|
||||
def odd?
|
||||
@odd_or_even = !@odd_or_even
|
||||
end
|
||||
|
||||
def logger
|
||||
ActiveRecord::Base.logger
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActiveRecord::LogSubscriber.attach_to :active_record
|
|
@ -26,9 +26,6 @@ module ActiveRecord
|
|||
load "active_record/railties/databases.rake"
|
||||
end
|
||||
|
||||
require "active_record/railties/log_subscriber"
|
||||
log_subscriber :active_record, ActiveRecord::Railties::LogSubscriber.new
|
||||
|
||||
initializer "active_record.initialize_timezone" do
|
||||
ActiveSupport.on_load(:active_record) do
|
||||
self.time_zone_aware_attributes = true
|
||||
|
|
|
@ -1,32 +0,0 @@
|
|||
module ActiveRecord
|
||||
module Railties
|
||||
class LogSubscriber < Rails::LogSubscriber
|
||||
def initialize
|
||||
super
|
||||
@odd_or_even = false
|
||||
end
|
||||
|
||||
def sql(event)
|
||||
name = '%s (%.1fms)' % [event.payload[:name], event.duration]
|
||||
sql = event.payload[:sql].squeeze(' ')
|
||||
|
||||
if odd?
|
||||
name = color(name, :cyan, true)
|
||||
sql = color(sql, nil, true)
|
||||
else
|
||||
name = color(name, :magenta, true)
|
||||
end
|
||||
|
||||
debug " #{name} #{sql}"
|
||||
end
|
||||
|
||||
def odd?
|
||||
@odd_or_even = !@odd_or_even
|
||||
end
|
||||
|
||||
def logger
|
||||
ActiveRecord::Base.logger
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,20 +1,19 @@
|
|||
require "cases/helper"
|
||||
require "models/developer"
|
||||
require "rails/log_subscriber/test_helper"
|
||||
require "active_record/railties/log_subscriber"
|
||||
require "active_support/log_subscriber/test_helper"
|
||||
|
||||
class LogSubscriberTest < ActiveSupport::TestCase
|
||||
include Rails::LogSubscriber::TestHelper
|
||||
include ActiveSupport::LogSubscriber::TestHelper
|
||||
|
||||
def setup
|
||||
@old_logger = ActiveRecord::Base.logger
|
||||
super
|
||||
|
||||
Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new)
|
||||
ActiveRecord::LogSubscriber.attach_to(:active_record)
|
||||
end
|
||||
|
||||
def teardown
|
||||
super
|
||||
ActiveRecord::LogSubscriber.log_subscribers.pop
|
||||
ActiveRecord::Base.logger = @old_logger
|
||||
end
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ require 'active_resource/exceptions'
|
|||
require 'active_resource/connection'
|
||||
require 'active_resource/formats'
|
||||
require 'active_resource/schema'
|
||||
require 'active_resource/log_subscriber'
|
||||
|
||||
module ActiveResource
|
||||
# ActiveResource::Base is the main class for mapping RESTful resources as models in a Rails application.
|
||||
|
|
15
activeresource/lib/active_resource/log_subscriber.rb
Normal file
15
activeresource/lib/active_resource/log_subscriber.rb
Normal file
|
@ -0,0 +1,15 @@
|
|||
module ActiveResource
|
||||
class LogSubscriber < ActiveSupport::LogSubscriber
|
||||
def request(event)
|
||||
result = event.payload[:result]
|
||||
info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}"
|
||||
info "--> %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration]
|
||||
end
|
||||
|
||||
def logger
|
||||
ActiveResource::Base.logger
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
ActiveResource::LogSubscriber.attach_to :active_resource
|
|
@ -5,9 +5,6 @@ module ActiveResource
|
|||
class Railtie < Rails::Railtie
|
||||
config.active_resource = ActiveSupport::OrderedOptions.new
|
||||
|
||||
require "active_resource/railties/log_subscriber"
|
||||
log_subscriber :active_resource, ActiveResource::Railties::LogSubscriber.new
|
||||
|
||||
initializer "active_resource.set_configs" do |app|
|
||||
app.config.active_resource.each do |k,v|
|
||||
ActiveResource::Base.send "#{k}=", v
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
module ActiveResource
|
||||
module Railties
|
||||
class LogSubscriber < Rails::LogSubscriber
|
||||
def request(event)
|
||||
result = event.payload[:result]
|
||||
info "#{event.payload[:method].to_s.upcase} #{event.payload[:request_uri]}"
|
||||
info "--> %d %s %d (%.1fms)" % [result.code, result.message, result.body.to_s.length, event.duration]
|
||||
end
|
||||
|
||||
def logger
|
||||
ActiveResource::Base.logger
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,12 +1,11 @@
|
|||
require "abstract_unit"
|
||||
require "fixtures/person"
|
||||
require "rails/log_subscriber/test_helper"
|
||||
require "active_resource/railties/log_subscriber"
|
||||
require "active_support/log_subscriber/test_helper"
|
||||
require "active_resource/log_subscriber"
|
||||
require "active_support/core_ext/hash/conversions"
|
||||
|
||||
# TODO: This test should be part of Railties
|
||||
class LogSubscriberTest < ActiveSupport::TestCase
|
||||
include Rails::LogSubscriber::TestHelper
|
||||
include ActiveSupport::LogSubscriber::TestHelper
|
||||
|
||||
def setup
|
||||
super
|
||||
|
@ -16,7 +15,7 @@ class LogSubscriberTest < ActiveSupport::TestCase
|
|||
mock.get "/people/1.xml", {}, @matz
|
||||
end
|
||||
|
||||
Rails::LogSubscriber.add(:active_resource, ActiveResource::Railties::LogSubscriber.new)
|
||||
ActiveResource::LogSubscriber.attach_to :active_resource
|
||||
end
|
||||
|
||||
def set_logger(logger)
|
||||
|
|
|
@ -41,6 +41,8 @@ module ActiveSupport
|
|||
|
||||
autoload :DescendantsTracker
|
||||
autoload :FileUpdateChecker
|
||||
autoload :LogSubscriber
|
||||
autoload :Notifications
|
||||
|
||||
# TODO: Narrow this list down
|
||||
eager_autoload do
|
||||
|
@ -64,7 +66,6 @@ module ActiveSupport
|
|||
autoload :OptionMerger
|
||||
autoload :OrderedHash
|
||||
autoload :OrderedOptions
|
||||
autoload :Notifications
|
||||
autoload :Rescuable
|
||||
autoload :SecureRandom
|
||||
autoload :StringInquirer
|
||||
|
|
|
@ -1,58 +1,62 @@
|
|||
require 'active_support/core_ext/class/inheritable_attributes'
|
||||
require 'active_support/notifications'
|
||||
require 'active_support/core_ext/module/attribute_accessors'
|
||||
require 'active_support/core_ext/class/attribute'
|
||||
|
||||
module Rails
|
||||
# Rails::LogSubscriber is an object set to consume ActiveSupport::Notifications
|
||||
# on initialization with solely purpose of logging. The log subscriber dispatches
|
||||
# notifications to a regirested object based on its given namespace.
|
||||
module ActiveSupport
|
||||
# ActiveSupport::LogSubscriber is an object set to consume ActiveSupport::Notifications
|
||||
# with solely purpose of logging. The log subscriber dispatches notifications to a
|
||||
# regirested object based on its given namespace.
|
||||
#
|
||||
# An example would be Active Record log subscriber responsible for logging queries:
|
||||
#
|
||||
# module ActiveRecord
|
||||
# class Railtie
|
||||
# class LogSubscriber < Rails::LogSubscriber
|
||||
# def sql(event)
|
||||
# "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}"
|
||||
# end
|
||||
# class LogSubscriber < ActiveSupport::LogSubscriber
|
||||
# def sql(event)
|
||||
# "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}"
|
||||
# end
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# It's finally registed as:
|
||||
# And it's finally registed as:
|
||||
#
|
||||
# Rails::LogSubscriber.add :active_record, ActiveRecord::Railtie::LogSubscriber.new
|
||||
# ActiveRecord::LogSubscriber.attach_to :active_record
|
||||
#
|
||||
# So whenever a "sql.active_record" notification arrive to Rails::LogSubscriber,
|
||||
# Since we need to know all instance methods before attaching the log subscriber,
|
||||
# the line above shuold be called after your ActiveRecord::LogSubscriber definition.
|
||||
#
|
||||
# After configured, whenever a "sql.active_record" notification is published,
|
||||
# it will properly dispatch the event (ActiveSupport::Notifications::Event) to
|
||||
# the sql method.
|
||||
#
|
||||
# This is useful because it avoids spanning several log subscribers just for logging
|
||||
# purposes(which slows down the main thread). Besides of providing a centralized
|
||||
# facility on top of Rails.logger.
|
||||
#
|
||||
# Log subscriber also has some helpers to deal with logging and automatically flushes
|
||||
# all logs when the request finishes (via action_dispatch.callback notification).
|
||||
# all logs when the request finishes (via action_dispatch.callback notification) in
|
||||
# a Rails environment.
|
||||
class LogSubscriber
|
||||
mattr_accessor :colorize_logging
|
||||
self.colorize_logging = true
|
||||
|
||||
class_attribute :logger
|
||||
|
||||
def self.logger
|
||||
@logger ||= Rails.logger if defined?(Rails)
|
||||
end
|
||||
|
||||
# Embed in a String to clear all previous ANSI sequences.
|
||||
CLEAR = "\e[0m"
|
||||
BOLD = "\e[1m"
|
||||
|
||||
CLEAR = "\e[0m"
|
||||
BOLD = "\e[1m"
|
||||
|
||||
# Colors
|
||||
BLACK = "\e[30m"
|
||||
RED = "\e[31m"
|
||||
GREEN = "\e[32m"
|
||||
YELLOW = "\e[33m"
|
||||
BLUE = "\e[34m"
|
||||
MAGENTA = "\e[35m"
|
||||
CYAN = "\e[36m"
|
||||
WHITE = "\e[37m"
|
||||
BLACK = "\e[30m"
|
||||
RED = "\e[31m"
|
||||
GREEN = "\e[32m"
|
||||
YELLOW = "\e[33m"
|
||||
BLUE = "\e[34m"
|
||||
MAGENTA = "\e[35m"
|
||||
CYAN = "\e[36m"
|
||||
WHITE = "\e[37m"
|
||||
|
||||
def self.add(namespace, log_subscriber, notifier = ActiveSupport::Notifications)
|
||||
def self.attach_to(namespace, log_subscriber=new, notifier=ActiveSupport::Notifications)
|
||||
log_subscribers << log_subscriber
|
||||
@flushable_loggers = nil
|
||||
@@flushable_loggers = nil
|
||||
|
||||
log_subscriber.public_methods(false).each do |event|
|
||||
notifier.subscribe("#{event}.#{namespace}") do |*args|
|
||||
|
@ -61,18 +65,18 @@ module Rails
|
|||
begin
|
||||
log_subscriber.send(event, ActiveSupport::Notifications::Event.new(*args))
|
||||
rescue Exception => e
|
||||
Rails.logger.error "Could not log #{args[0].inspect} event. #{e.class}: #{e.message}"
|
||||
log_subscriber.logger.error "Could not log #{args[0].inspect} event. #{e.class}: #{e.message}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def self.log_subscribers
|
||||
@log_subscribers ||= []
|
||||
@@log_subscribers ||= []
|
||||
end
|
||||
|
||||
def self.flushable_loggers
|
||||
@flushable_loggers ||= begin
|
||||
@@flushable_loggers ||= begin
|
||||
loggers = log_subscribers.map(&:logger)
|
||||
loggers.uniq!
|
||||
loggers.select { |l| l.respond_to?(:flush) }
|
||||
|
@ -84,11 +88,6 @@ module Rails
|
|||
flushable_loggers.each(&:flush)
|
||||
end
|
||||
|
||||
# By default, we use the Rails.logger for logging.
|
||||
def logger
|
||||
Rails.logger
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
%w(info debug warn error fatal unknown).each do |level|
|
|
@ -1,13 +1,16 @@
|
|||
require 'rails/log_subscriber'
|
||||
require 'active_support/log_subscriber'
|
||||
|
||||
module Rails
|
||||
module ActiveSupport
|
||||
class LogSubscriber
|
||||
# Provides some helpers to deal with testing log subscribers by setting up
|
||||
# notifications. Take for instance Active Record subscriber tests:
|
||||
#
|
||||
# class SyncLogSubscriberTest < ActiveSupport::TestCase
|
||||
# include Rails::LogSubscriber::TestHelper
|
||||
# Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new)
|
||||
# include ActiveSupport::LogSubscriber::TestHelper
|
||||
#
|
||||
# def setup
|
||||
# ActiveRecord::LogSubscriber.attach_to(:active_record)
|
||||
# end
|
||||
#
|
||||
# def test_basic_query_logging
|
||||
# Developer.all
|
||||
|
@ -16,16 +19,6 @@ module Rails
|
|||
# assert_match /Developer Load/, @logger.logged(:debug).last
|
||||
# assert_match /SELECT \* FROM "developers"/, @logger.logged(:debug).last
|
||||
# end
|
||||
#
|
||||
# class SyncLogSubscriberTest < ActiveSupport::TestCase
|
||||
# include Rails::LogSubscriber::SyncTestHelper
|
||||
# include LogSubscriberTest
|
||||
# end
|
||||
#
|
||||
# class AsyncLogSubscriberTest < ActiveSupport::TestCase
|
||||
# include Rails::LogSubscriber::AsyncTestHelper
|
||||
# include LogSubscriberTest
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# All you need to do is to ensure that your log subscriber is added to Rails::Subscriber,
|
||||
|
@ -42,7 +35,7 @@ module Rails
|
|||
@logger = MockLogger.new
|
||||
@notifier = ActiveSupport::Notifications::Notifier.new(queue)
|
||||
|
||||
Rails::LogSubscriber.colorize_logging = false
|
||||
ActiveSupport::LogSubscriber.colorize_logging = false
|
||||
|
||||
set_logger(@logger)
|
||||
ActiveSupport::Notifications.notifier = @notifier
|
||||
|
@ -86,7 +79,7 @@ module Rails
|
|||
# end
|
||||
#
|
||||
def set_logger(logger)
|
||||
Rails.logger = logger
|
||||
ActiveSupport::LogSubscriber.logger = logger
|
||||
end
|
||||
|
||||
def queue
|
|
@ -1,7 +1,7 @@
|
|||
require 'abstract_unit'
|
||||
require 'rails/log_subscriber/test_helper'
|
||||
require 'active_support/log_subscriber/test_helper'
|
||||
|
||||
class MyLogSubscriber < Rails::LogSubscriber
|
||||
class MyLogSubscriber < ActiveSupport::LogSubscriber
|
||||
attr_reader :event
|
||||
|
||||
def some_event(event)
|
||||
|
@ -25,7 +25,7 @@ class MyLogSubscriber < Rails::LogSubscriber
|
|||
end
|
||||
|
||||
class SyncLogSubscriberTest < ActiveSupport::TestCase
|
||||
include Rails::LogSubscriber::TestHelper
|
||||
include ActiveSupport::LogSubscriber::TestHelper
|
||||
|
||||
def setup
|
||||
super
|
||||
|
@ -34,7 +34,7 @@ class SyncLogSubscriberTest < ActiveSupport::TestCase
|
|||
|
||||
def teardown
|
||||
super
|
||||
Rails::LogSubscriber.log_subscribers.clear
|
||||
ActiveSupport::LogSubscriber.log_subscribers.clear
|
||||
end
|
||||
|
||||
def instrument(*args, &block)
|
||||
|
@ -49,7 +49,7 @@ class SyncLogSubscriberTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
def test_set_color_for_messages
|
||||
Rails::LogSubscriber.colorize_logging = true
|
||||
ActiveSupport::LogSubscriber.colorize_logging = true
|
||||
@log_subscriber.bar(nil)
|
||||
assert_equal "\e[31mcool\e[0m, \e[1m\e[34misn't it?\e[0m", @logger.logged(:info).last
|
||||
end
|
||||
|
@ -60,56 +60,56 @@ class SyncLogSubscriberTest < ActiveSupport::TestCase
|
|||
end
|
||||
|
||||
def test_event_is_sent_to_the_registered_class
|
||||
Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
|
||||
instrument "some_event.my_log_subscriber"
|
||||
wait
|
||||
assert_equal %w(some_event.my_log_subscriber), @logger.logged(:info)
|
||||
end
|
||||
|
||||
def test_event_is_an_active_support_notifications_event
|
||||
Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
|
||||
instrument "some_event.my_log_subscriber"
|
||||
wait
|
||||
assert_kind_of ActiveSupport::Notifications::Event, @log_subscriber.event
|
||||
end
|
||||
|
||||
def test_does_not_send_the_event_if_it_doesnt_match_the_class
|
||||
Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
|
||||
instrument "unknown_event.my_log_subscriber"
|
||||
wait
|
||||
# If we get here, it means that NoMethodError was not raised.
|
||||
end
|
||||
|
||||
def test_does_not_send_the_event_if_logger_is_nil
|
||||
Rails.logger = nil
|
||||
ActiveSupport::LogSubscriber.logger = nil
|
||||
@log_subscriber.expects(:some_event).never
|
||||
Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
|
||||
instrument "some_event.my_log_subscriber"
|
||||
wait
|
||||
end
|
||||
|
||||
def test_does_not_fail_with_non_namespaced_events
|
||||
Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
|
||||
instrument "whatever"
|
||||
wait
|
||||
end
|
||||
|
||||
def test_flushes_loggers
|
||||
Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber
|
||||
Rails::LogSubscriber.flush_all!
|
||||
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.flush_all!
|
||||
assert_equal 1, @logger.flush_count
|
||||
end
|
||||
|
||||
def test_flushes_the_same_logger_just_once
|
||||
Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber
|
||||
Rails::LogSubscriber.add :another, @log_subscriber
|
||||
Rails::LogSubscriber.flush_all!
|
||||
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.attach_to :another, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.flush_all!
|
||||
wait
|
||||
assert_equal 1, @logger.flush_count
|
||||
end
|
||||
|
||||
def test_logging_does_not_die_on_failures
|
||||
Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber
|
||||
ActiveSupport::LogSubscriber.attach_to :my_log_subscriber, @log_subscriber
|
||||
instrument "puke.my_log_subscriber"
|
||||
instrument "some_event.my_log_subscriber"
|
||||
wait
|
|
@ -9,7 +9,6 @@ require 'active_support/core_ext/logger'
|
|||
require 'rails/application'
|
||||
require 'rails/version'
|
||||
require 'rails/deprecation'
|
||||
require 'rails/log_subscriber'
|
||||
|
||||
require 'active_support/railtie'
|
||||
require 'action_dispatch/railtie'
|
||||
|
|
|
@ -110,7 +110,7 @@ module Rails
|
|||
|
||||
def colorize_logging=(val)
|
||||
@colorize_logging = val
|
||||
Rails::LogSubscriber.colorize_logging = val
|
||||
ActiveSupport::LogSubscriber.colorize_logging = val
|
||||
self.generators.colorize_logging = val
|
||||
end
|
||||
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
require 'rails/log_subscriber'
|
||||
require 'active_support/core_ext/time/conversions'
|
||||
|
||||
module Rails
|
||||
module Rack
|
||||
# Log the request started and flush all loggers after it.
|
||||
class Logger < Rails::LogSubscriber
|
||||
class Logger < ActiveSupport::LogSubscriber
|
||||
def initialize(app)
|
||||
@app = app
|
||||
end
|
||||
|
@ -16,20 +15,19 @@ module Rails
|
|||
after_dispatch(env)
|
||||
end
|
||||
|
||||
protected
|
||||
protected
|
||||
|
||||
def before_dispatch(env)
|
||||
request = ActionDispatch::Request.new(env)
|
||||
path = request.fullpath
|
||||
def before_dispatch(env)
|
||||
request = ActionDispatch::Request.new(env)
|
||||
path = request.fullpath
|
||||
|
||||
info "\n\nStarted #{env["REQUEST_METHOD"]} \"#{path}\" " \
|
||||
"for #{request.ip} at #{Time.now.to_default_s}"
|
||||
end
|
||||
|
||||
def after_dispatch(env)
|
||||
Rails::LogSubscriber.flush_all!
|
||||
end
|
||||
info "\n\nStarted #{env["REQUEST_METHOD"]} \"#{path}\" " \
|
||||
"for #{request.ip} at #{Time.now.to_default_s}"
|
||||
end
|
||||
|
||||
def after_dispatch(env)
|
||||
ActiveSupport::LogSubscriber.flush_all!
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -114,36 +114,6 @@ module Rails
|
|||
# end
|
||||
# end
|
||||
#
|
||||
# == Adding your subscriber
|
||||
#
|
||||
# Since version 3.0, Rails ships with a notification system which is used for several
|
||||
# purposes, including logging. If you are sending notifications in your Railtie, you may
|
||||
# want to add a subscriber to consume such notifications for logging purposes.
|
||||
#
|
||||
# The subscriber is added under the railtie_name namespace and only consumes notifications
|
||||
# under the given namespace. For example, let's suppose your railtie is publishing the
|
||||
# following "something_expensive" instrumentation:
|
||||
#
|
||||
# ActiveSupport::Notifications.instrument "my_railtie.something_expensive" do
|
||||
# # something expensive
|
||||
# end
|
||||
#
|
||||
# You can log this instrumentation with your own Rails::Subscriber:
|
||||
#
|
||||
# class MyRailtie::Subscriber < Rails::Subscriber
|
||||
# def something_expensive(event)
|
||||
# info("Something expensive took %.1fms" % event.duration)
|
||||
# end
|
||||
# end
|
||||
#
|
||||
# By registering it:
|
||||
#
|
||||
# class MyRailtie < Railtie
|
||||
# subscriber :my_gem, MyRailtie::Subscriber.new
|
||||
# end
|
||||
#
|
||||
# Take a look in Rails::Subscriber docs for more information.
|
||||
#
|
||||
# == Application, Plugin and Engine
|
||||
#
|
||||
# A Rails::Engine is nothing more than a Railtie with some initializers already set.
|
||||
|
@ -176,8 +146,8 @@ module Rails
|
|||
ActiveSupport::Deprecation.warn "railtie_name is deprecated and has no effect", caller
|
||||
end
|
||||
|
||||
def log_subscriber(name, log_subscriber)
|
||||
Rails::LogSubscriber.add(name, log_subscriber)
|
||||
def log_subscriber(*)
|
||||
ActiveSupport::Deprecation.warn "log_subscriber is deprecated and has no effect", caller
|
||||
end
|
||||
|
||||
def rake_tasks(&blk)
|
||||
|
|
Loading…
Reference in a new issue