Rails::Subscriber is now responsible for flushing all loggers it's responsible for.
This commit is contained in:
parent
8b9bff9557
commit
b4019d5080
|
@ -30,7 +30,8 @@ module Rails
|
||||||
# purposes(which slows down the main thread). Besides of providing a centralized
|
# purposes(which slows down the main thread). Besides of providing a centralized
|
||||||
# facility on top of Rails.logger.
|
# 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
|
class Subscriber
|
||||||
cattr_accessor :colorize_logging, :instance_writer => false
|
cattr_accessor :colorize_logging, :instance_writer => false
|
||||||
self.colorize_logging = true
|
self.colorize_logging = true
|
||||||
|
@ -64,6 +65,15 @@ module Rails
|
||||||
if subscriber.respond_to?(name) && subscriber.logger
|
if subscriber.respond_to?(name) && subscriber.logger
|
||||||
subscriber.send(name, ActiveSupport::Notifications::Event.new(*args))
|
subscriber.send(name, ActiveSupport::Notifications::Event.new(*args))
|
||||||
end
|
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
|
end
|
||||||
|
|
||||||
# By default, we use the Rails.logger for logging.
|
# By default, we use the Rails.logger for logging.
|
||||||
|
|
|
@ -58,7 +58,10 @@ module Rails
|
||||||
end
|
end
|
||||||
|
|
||||||
class MockLogger
|
class MockLogger
|
||||||
|
attr_reader :flush_count
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
|
@flush_count = 0
|
||||||
@logged = Hash.new { |h,k| h[k] = [] }
|
@logged = Hash.new { |h,k| h[k] = [] }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -69,6 +72,10 @@ module Rails
|
||||||
def logged(level)
|
def logged(level)
|
||||||
@logged[level].compact.map { |l| l.to_s.strip }
|
@logged[level].compact.map { |l| l.to_s.strip }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def flush
|
||||||
|
@flush_count += 1
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# Wait notifications to be published.
|
# Wait notifications to be published.
|
||||||
|
|
|
@ -76,10 +76,31 @@ module SubscriberTest
|
||||||
|
|
||||||
def test_does_not_send_the_event_if_logger_is_nil
|
def test_does_not_send_the_event_if_logger_is_nil
|
||||||
Rails.logger = nil
|
Rails.logger = nil
|
||||||
|
@subscriber.expects(:some_event).never
|
||||||
Rails::Subscriber.add :my_subscriber, @subscriber
|
Rails::Subscriber.add :my_subscriber, @subscriber
|
||||||
instrument "my_subscriber.some_event"
|
instrument "my_subscriber.some_event"
|
||||||
wait
|
wait
|
||||||
assert_equal [], @logger.logged(:info)
|
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_loggers_when_action_dispatch_callback_is_received
|
||||||
|
Rails::Subscriber.add :my_subscriber, @subscriber
|
||||||
|
instrument "action_dispatch.callback"
|
||||||
|
wait
|
||||||
|
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
|
||||||
|
instrument "action_dispatch.callback"
|
||||||
|
wait
|
||||||
|
assert_equal 1, @logger.flush_count
|
||||||
end
|
end
|
||||||
|
|
||||||
class SyncSubscriberTest < ActiveSupport::TestCase
|
class SyncSubscriberTest < ActiveSupport::TestCase
|
||||||
|
|
Loading…
Reference in New Issue