diff options
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/3_0_release_notes.textile | 2 | ||||
-rw-r--r-- | railties/lib/rails.rb | 2 | ||||
-rw-r--r-- | railties/lib/rails/application/bootstrap.rb | 8 | ||||
-rw-r--r-- | railties/lib/rails/log_subscriber.rb (renamed from railties/lib/rails/subscriber.rb) | 36 | ||||
-rw-r--r-- | railties/lib/rails/log_subscriber/test_helper.rb (renamed from railties/lib/rails/subscriber/test_helper.rb) | 32 | ||||
-rw-r--r-- | railties/lib/rails/rack/logger.rb | 6 | ||||
-rw-r--r-- | railties/lib/rails/railtie.rb | 4 | ||||
-rw-r--r-- | railties/test/application/initializers/notifications_test.rb | 2 | ||||
-rw-r--r-- | railties/test/log_subscriber_test.rb | 119 | ||||
-rw-r--r-- | railties/test/railties/railtie_test.rb | 8 | ||||
-rw-r--r-- | railties/test/subscriber_test.rb | 119 |
11 files changed, 169 insertions, 169 deletions
diff --git a/railties/guides/source/3_0_release_notes.textile b/railties/guides/source/3_0_release_notes.textile index 639e775bf5..4b0dc72908 100644 --- a/railties/guides/source/3_0_release_notes.textile +++ b/railties/guides/source/3_0_release_notes.textile @@ -435,7 +435,7 @@ As well as the following deprecations: * I18n error messages for ActiveRecord should be changed from :en.activerecord.errors.template to <tt>:en.errors.template</tt>. * <tt>model.errors.on</tt> is deprecated in favour of <tt>model.errors[]</tt> * validates_presence_of => validates... :presence => true -* <tt>ActiveRecord::Base.colorize_logging</tt> and <tt>config.active_record.colorize_logging</tt> are deprecated in favour of <tt>Rails::Subscriber.colorize_logging</tt> or <tt>config.colorize_logging</tt> +* <tt>ActiveRecord::Base.colorize_logging</tt> and <tt>config.active_record.colorize_logging</tt> are deprecated in favour of <tt>Rails::LogSubscriber.colorize_logging</tt> or <tt>config.colorize_logging</tt> NOTE: While an implementation of State Machine has been in Active Record edge for some months now, it has been removed from the Rails 3.0 release. diff --git a/railties/lib/rails.rb b/railties/lib/rails.rb index b7a39fd5a7..3d3151bd8f 100644 --- a/railties/lib/rails.rb +++ b/railties/lib/rails.rb @@ -7,7 +7,7 @@ require 'active_support/core_ext/logger' require 'rails/application' require 'rails/version' require 'rails/deprecation' -require 'rails/subscriber' +require 'rails/log_subscriber' require 'rails/ruby_version_check' require 'active_support/railtie' diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index b20e53f2de..0714b34b31 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -49,17 +49,17 @@ module Rails end end - # Initialize rails subscriber on top of notifications. - initializer :initialize_subscriber do + # Initialize rails log subscriber on top of notifications. + initializer :initialize_log_subscriber do require 'active_support/notifications' if config.colorize_logging == false - Rails::Subscriber.colorize_logging = false + Rails::LogSubscriber.colorize_logging = false config.generators.colorize_logging = false end ActiveSupport::Notifications.subscribe do |*args| - Rails::Subscriber.dispatch(args) + Rails::LogSubscriber.dispatch(args) end end diff --git a/railties/lib/rails/subscriber.rb b/railties/lib/rails/log_subscriber.rb index 8c62f562d9..c867b92b6b 100644 --- a/railties/lib/rails/subscriber.rb +++ b/railties/lib/rails/log_subscriber.rb @@ -2,15 +2,15 @@ require 'active_support/core_ext/class/inheritable_attributes' require 'active_support/notifications' module Rails - # Rails::Subscriber is an object set to consume ActiveSupport::Notifications - # on initialization with solely purpose of logging. The subscriber dispatches + # 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. # - # An example would be ActiveRecord subscriber responsible for logging queries: + # An example would be ActiveRecord log subscriber responsible for logging queries: # # module ActiveRecord # class Railtie - # class Subscriber < Rails::Subscriber + # class LogSubscriber < Rails::LogSubscriber # def sql(event) # "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}" # end @@ -20,19 +20,19 @@ module Rails # # It's finally registed as: # - # Rails::Subscriber.add :active_record, ActiveRecord::Railtie::Subscriber.new + # Rails::LogSubscriber.add :active_record, ActiveRecord::Railtie::LogSubscriber.new # - # So whenever a "active_record.sql" notification arrive to Rails::Subscriber, + # So whenever a "active_record.sql" notification arrive to Rails::LogSubscriber, # it will properly dispatch the event (ActiveSupport::Notifications::Event) to # the sql method. # - # This is useful because it avoids spanning several subscribers just for logging + # 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. # - # Subscriber also has some helpers to deal with logging and automatically flushes + # Log subscriber also has some helpers to deal with logging and automatically flushes # all logs when the request finishes (via action_dispatch.callback notification). - class Subscriber + class LogSubscriber mattr_accessor :colorize_logging self.colorize_logging = true @@ -50,30 +50,30 @@ module Rails CYAN = "\e[36m" WHITE = "\e[37m" - def self.add(namespace, subscriber) - subscribers[namespace.to_sym] = subscriber + def self.add(namespace, log_subscriber) + log_subscribers[namespace.to_sym] = log_subscriber end - def self.subscribers - @subscribers ||= {} + def self.log_subscribers + @log_subscribers ||= {} end def self.dispatch(args) namespace, name = args[0].split(".") - subscriber = subscribers[namespace.to_sym] + log_subscriber = log_subscribers[namespace.to_sym] - if subscriber.respond_to?(name) && subscriber.logger + if log_subscriber.respond_to?(name) && log_subscriber.logger begin - subscriber.send(name, ActiveSupport::Notifications::Event.new(*args)) + log_subscriber.send(name, ActiveSupport::Notifications::Event.new(*args)) rescue Exception => e Rails.logger.error "Could not log #{args[0].inspect} event. #{e.class}: #{e.message}" end end end - # Flush all subscribers' logger. + # Flush all log_subscribers' logger. def self.flush_all! - loggers = subscribers.values.map(&:logger) + loggers = log_subscribers.values.map(&:logger) loggers.uniq! loggers.each { |l| l.flush if l.respond_to?(:flush) } end diff --git a/railties/lib/rails/subscriber/test_helper.rb b/railties/lib/rails/log_subscriber/test_helper.rb index 39b4117372..9ede56cad4 100644 --- a/railties/lib/rails/subscriber/test_helper.rb +++ b/railties/lib/rails/log_subscriber/test_helper.rb @@ -1,13 +1,13 @@ -require 'rails/subscriber' +require 'rails/log_subscriber' module Rails - class Subscriber - # Provides some helpers to deal with testing subscribers by setting up + class LogSubscriber + # Provides some helpers to deal with testing log subscribers by setting up # notifications. Take for instance ActiveRecord subscriber tests: # - # class SyncSubscriberTest < ActiveSupport::TestCase - # include Rails::Subscriber::TestHelper - # Rails::Subscriber.add(:active_record, ActiveRecord::Railties::Subscriber.new) + # class SyncLogSubscriberTest < ActiveSupport::TestCase + # include Rails::LogSubscriber::TestHelper + # Rails::LogSubscriber.add(:active_record, ActiveRecord::Railties::LogSubscriber.new) # # def test_basic_query_logging # Developer.all @@ -17,18 +17,18 @@ module Rails # assert_match /SELECT \* FROM "developers"/, @logger.logged(:debug).last # end # - # class SyncSubscriberTest < ActiveSupport::TestCase - # include Rails::Subscriber::SyncTestHelper - # include SubscriberTest + # class SyncLogSubscriberTest < ActiveSupport::TestCase + # include Rails::LogSubscriber::SyncTestHelper + # include LogSubscriberTest # end # - # class AsyncSubscriberTest < ActiveSupport::TestCase - # include Rails::Subscriber::AsyncTestHelper - # include SubscriberTest + # class AsyncLogSubscriberTest < ActiveSupport::TestCase + # include Rails::LogSubscriber::AsyncTestHelper + # include LogSubscriberTest # end # end # - # All you need to do is to ensure that your subscriber is added to Rails::Subscriber, + # All you need to do is to ensure that your log subscriber is added to Rails::Subscriber, # as in the second line of the code above. The test helpers is reponsible for setting # up the queue, subscriptions and turning colors in logs off. # @@ -42,8 +42,8 @@ module Rails @logger = MockLogger.new @notifier = ActiveSupport::Notifications::Notifier.new(queue) - Rails::Subscriber.colorize_logging = false - @notifier.subscribe { |*args| Rails::Subscriber.dispatch(args) } + Rails::LogSubscriber.colorize_logging = false + @notifier.subscribe { |*args| Rails::LogSubscriber.dispatch(args) } set_logger(@logger) ActiveSupport::Notifications.notifier = @notifier @@ -80,7 +80,7 @@ module Rails @notifier.wait end - # Overwrite if you use another logger in your subscriber: + # Overwrite if you use another logger in your log subscriber: # # def logger # ActiveRecord::Base.logger = @logger diff --git a/railties/lib/rails/rack/logger.rb b/railties/lib/rails/rack/logger.rb index de21fb4f10..2efe224e94 100644 --- a/railties/lib/rails/rack/logger.rb +++ b/railties/lib/rails/rack/logger.rb @@ -1,9 +1,9 @@ -require 'rails/subscriber' +require 'rails/log_subscriber' module Rails module Rack # Log the request started and flush all loggers after it. - class Logger < Rails::Subscriber + class Logger < Rails::LogSubscriber def initialize(app) @app = app end @@ -26,7 +26,7 @@ module Rails end def after_dispatch(env) - Rails::Subscriber.flush_all! + Rails::LogSubscriber.flush_all! end end diff --git a/railties/lib/rails/railtie.rb b/railties/lib/rails/railtie.rb index c038d0ac70..67afae5862 100644 --- a/railties/lib/rails/railtie.rb +++ b/railties/lib/rails/railtie.rb @@ -32,8 +32,8 @@ module Rails subclasses.map { |p| p.railtie_name } end - def subscriber(subscriber) - Rails::Subscriber.add(railtie_name, subscriber) + def log_subscriber(log_subscriber) + Rails::LogSubscriber.add(railtie_name, log_subscriber) end def rake_tasks(&blk) diff --git a/railties/test/application/initializers/notifications_test.rb b/railties/test/application/initializers/notifications_test.rb index 061bb34c19..b99cf5bb4f 100644 --- a/railties/test/application/initializers/notifications_test.rb +++ b/railties/test/application/initializers/notifications_test.rb @@ -28,7 +28,7 @@ module ApplicationTests ActiveSupport::Notifications.notifier.wait end - test "rails subscribers are added" do + test "rails log_subscribers are added" do add_to_config <<-RUBY config.colorize_logging = false RUBY diff --git a/railties/test/log_subscriber_test.rb b/railties/test/log_subscriber_test.rb new file mode 100644 index 0000000000..be176df1bb --- /dev/null +++ b/railties/test/log_subscriber_test.rb @@ -0,0 +1,119 @@ +require 'abstract_unit' +require 'rails/log_subscriber/test_helper' + +class MyLogSubscriber < Rails::LogSubscriber + attr_reader :event + + def some_event(event) + @event = event + info event.name + end + + def foo(event) + debug "debug" + info "info" + warn "warn" + end + + def bar(event) + info "#{color("cool", :red)}, #{color("isn't it?", :blue, true)}" + end + + def puke(event) + raise "puke" + end +end + +class SyncLogSubscriberTest < ActiveSupport::TestCase + include Rails::LogSubscriber::TestHelper + + def setup + super + @log_subscriber = MyLogSubscriber.new + Rails::LogSubscriber.instance_variable_set(:@log_tailer, nil) + end + + def teardown + super + Rails::LogSubscriber.log_subscribers.clear + Rails::LogSubscriber.instance_variable_set(:@log_tailer, nil) + end + + def instrument(*args, &block) + ActiveSupport::Notifications.instrument(*args, &block) + end + + def test_proxies_method_to_rails_logger + @log_subscriber.foo(nil) + assert_equal %w(debug), @logger.logged(:debug) + assert_equal %w(info), @logger.logged(:info) + assert_equal %w(warn), @logger.logged(:warn) + end + + def test_set_color_for_messages + Rails::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 + + def test_does_not_set_color_if_colorize_logging_is_set_to_false + @log_subscriber.bar(nil) + assert_equal "cool, isn't it?", @logger.logged(:info).last + end + + def test_event_is_sent_to_the_registered_class + Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber + instrument "my_log_subscriber.some_event" + wait + assert_equal %w(my_log_subscriber.some_event), @logger.logged(:info) + end + + def test_event_is_an_active_support_notifications_event + Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber + instrument "my_log_subscriber.some_event" + 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 + instrument "my_log_subscriber.unknown_event" + wait + # If we get here, it means that NoMethodError was raised. + end + + def test_does_not_send_the_event_if_logger_is_nil + Rails.logger = nil + @log_subscriber.expects(:some_event).never + Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber + instrument "my_log_subscriber.some_event" + wait + end + + def test_flushes_loggers + Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber + Rails::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! + wait + assert_equal 1, @logger.flush_count + end + + def test_logging_does_not_die_on_failures + Rails::LogSubscriber.add :my_log_subscriber, @log_subscriber + instrument "my_log_subscriber.puke" + instrument "my_log_subscriber.some_event" + wait + + assert_equal 1, @logger.logged(:info).size + assert_equal 'my_log_subscriber.some_event', @logger.logged(:info).last + + assert_equal 1, @logger.logged(:error).size + assert_equal 'Could not log "my_log_subscriber.puke" event. RuntimeError: puke', @logger.logged(:error).last + end +end
\ No newline at end of file diff --git a/railties/test/railties/railtie_test.rb b/railties/test/railties/railtie_test.rb index b723e08281..9eb4e9993a 100644 --- a/railties/test/railties/railtie_test.rb +++ b/railties/test/railties/railtie_test.rb @@ -51,12 +51,12 @@ module RailtiesTest assert_equal "bar", Bar.config.foo.bar end - test "railtie can add subscribers" do + test "railtie can add log subscribers" do begin - class Foo < Rails::Railtie ; subscriber(Rails::Subscriber.new) ; end - assert_kind_of Rails::Subscriber, Rails::Subscriber.subscribers[:foo] + class Foo < Rails::Railtie ; log_subscriber(Rails::LogSubscriber.new) ; end + assert_kind_of Rails::LogSubscriber, Rails::LogSubscriber.log_subscribers[:foo] ensure - Rails::Subscriber.subscribers.clear + Rails::LogSubscriber.log_subscribers.clear end end diff --git a/railties/test/subscriber_test.rb b/railties/test/subscriber_test.rb deleted file mode 100644 index f6c895093f..0000000000 --- a/railties/test/subscriber_test.rb +++ /dev/null @@ -1,119 +0,0 @@ -require 'abstract_unit' -require 'rails/subscriber/test_helper' - -class MySubscriber < Rails::Subscriber - attr_reader :event - - def some_event(event) - @event = event - info event.name - end - - def foo(event) - debug "debug" - info "info" - warn "warn" - end - - def bar(event) - info "#{color("cool", :red)}, #{color("isn't it?", :blue, true)}" - end - - def puke(event) - raise "puke" - end -end - -class SyncSubscriberTest < ActiveSupport::TestCase - include Rails::Subscriber::TestHelper - - def setup - super - @subscriber = MySubscriber.new - Rails::Subscriber.instance_variable_set(:@log_tailer, nil) - end - - def teardown - super - Rails::Subscriber.subscribers.clear - Rails::Subscriber.instance_variable_set(:@log_tailer, nil) - end - - def instrument(*args, &block) - ActiveSupport::Notifications.instrument(*args, &block) - end - - def test_proxies_method_to_rails_logger - @subscriber.foo(nil) - assert_equal %w(debug), @logger.logged(:debug) - assert_equal %w(info), @logger.logged(:info) - assert_equal %w(warn), @logger.logged(:warn) - end - - def test_set_color_for_messages - Rails::Subscriber.colorize_logging = true - @subscriber.bar(nil) - assert_equal "\e[31mcool\e[0m, \e[1m\e[34misn't it?\e[0m", @logger.logged(:info).last - end - - def test_does_not_set_color_if_colorize_logging_is_set_to_false - @subscriber.bar(nil) - assert_equal "cool, isn't it?", @logger.logged(:info).last - end - - def test_event_is_sent_to_the_registered_class - Rails::Subscriber.add :my_subscriber, @subscriber - instrument "my_subscriber.some_event" - wait - assert_equal %w(my_subscriber.some_event), @logger.logged(:info) - end - - def test_event_is_an_active_support_notifications_event - Rails::Subscriber.add :my_subscriber, @subscriber - instrument "my_subscriber.some_event" - wait - assert_kind_of ActiveSupport::Notifications::Event, @subscriber.event - end - - def test_does_not_send_the_event_if_it_doesnt_match_the_class - Rails::Subscriber.add :my_subscriber, @subscriber - instrument "my_subscriber.unknown_event" - wait - # If we get here, it means that NoMethodError was raised. - end - - def test_does_not_send_the_event_if_logger_is_nil - Rails.logger = nil - @subscriber.expects(:some_event).never - Rails::Subscriber.add :my_subscriber, @subscriber - instrument "my_subscriber.some_event" - wait - end - - def test_flushes_loggers - Rails::Subscriber.add :my_subscriber, @subscriber - Rails::Subscriber.flush_all! - assert_equal 1, @logger.flush_count - end - - def test_flushes_the_same_logger_just_once - Rails::Subscriber.add :my_subscriber, @subscriber - Rails::Subscriber.add :another, @subscriber - Rails::Subscriber.flush_all! - wait - assert_equal 1, @logger.flush_count - end - - def test_logging_does_not_die_on_failures - Rails::Subscriber.add :my_subscriber, @subscriber - instrument "my_subscriber.puke" - instrument "my_subscriber.some_event" - wait - - assert_equal 1, @logger.logged(:info).size - assert_equal 'my_subscriber.some_event', @logger.logged(:info).last - - assert_equal 1, @logger.logged(:error).size - assert_equal 'Could not log "my_subscriber.puke" event. RuntimeError: puke', @logger.logged(:error).last - end -end
\ No newline at end of file |