From b4019d5080181a911f8652cabd184794963911b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Thu, 14 Jan 2010 22:03:47 +0100 Subject: Rails::Subscriber is now responsible for flushing all loggers it's responsible for. --- railties/lib/rails/subscriber.rb | 12 +++++++++++- railties/lib/rails/subscriber/test_helper.rb | 7 +++++++ 2 files changed, 18 insertions(+), 1 deletion(-) (limited to 'railties/lib/rails') diff --git a/railties/lib/rails/subscriber.rb b/railties/lib/rails/subscriber.rb index f98dec738d..88a6f77eaa 100644 --- a/railties/lib/rails/subscriber.rb +++ b/railties/lib/rails/subscriber.rb @@ -30,7 +30,8 @@ module Rails # purposes(which slows down the main thread). Besides of providing a centralized # facility on top of Rails.logger. # - # Finally, Subscriber some helpers to deal with logging, like managing console colors. + # 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 cattr_accessor :colorize_logging, :instance_writer => false self.colorize_logging = true @@ -64,6 +65,15 @@ module Rails if subscriber.respond_to?(name) && subscriber.logger subscriber.send(name, ActiveSupport::Notifications::Event.new(*args)) end + + flush_all! if args[0] == "action_dispatch.callback" + end + + # Flush all subscribers' logger. + def self.flush_all! + loggers = subscribers.values.map(&:logger) + loggers.uniq! + loggers.each { |l| l.flush if l.respond_to?(:flush) } end # By default, we use the Rails.logger for logging. diff --git a/railties/lib/rails/subscriber/test_helper.rb b/railties/lib/rails/subscriber/test_helper.rb index 8b981dc3a6..1464767ed9 100644 --- a/railties/lib/rails/subscriber/test_helper.rb +++ b/railties/lib/rails/subscriber/test_helper.rb @@ -58,7 +58,10 @@ module Rails end class MockLogger + attr_reader :flush_count + def initialize + @flush_count = 0 @logged = Hash.new { |h,k| h[k] = [] } end @@ -69,6 +72,10 @@ module Rails def logged(level) @logged[level].compact.map { |l| l.to_s.strip } end + + def flush + @flush_count += 1 + end end # Wait notifications to be published. -- cgit v1.2.3