1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

LogTailer should be invoked after all logs in threads were flushed.

This commit is contained in:
José Valim 2010-01-15 10:46:30 +01:00
parent b4019d5080
commit b0994be5bd
4 changed files with 30 additions and 9 deletions

View file

@ -46,6 +46,7 @@ module Rails
trap(:INT) { exit }
puts "=> Ctrl-C to shutdown server" unless options[:daemonize]
Rails::Subscriber.tail_log = true unless options[:daemonize]
super
ensure
puts 'Exiting' unless options[:daemonize]
@ -53,15 +54,10 @@ module Rails
def middleware
middlewares = []
middlewares << [Rails::Rack::LogTailer, log_path] unless options[:daemonize]
middlewares << [Rails::Rack::Debugger] if options[:debugger]
Hash.new(middlewares)
end
def log_path
"log/#{options[:environment]}.log"
end
def default_options
super.merge({
:Port => 3000,

View file

@ -13,11 +13,11 @@ module Rails
def call(env)
response = @app.call(env)
tail_log
tail!
response
end
def tail_log
def tail!
@file.seek @cursor
mod = @file.mtime.to_f

View file

@ -33,8 +33,9 @@ module Rails
# 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
mattr_accessor :colorize_logging, :tail_log
self.colorize_logging = true
self.tail_log = false
# Embed in a String to clear all previous ANSI sequences.
CLEAR = "\e[0m"
@ -58,6 +59,12 @@ module Rails
@subscribers ||= {}
end
# Use Rails::Rack::LogTailer to do the log tailing.
# TODO Leave this as middleware or move inside Subscriber?
def self.log_tailer
@log_tailer ||= Rails::Rack::LogTailer.new(nil, "log/#{Rails.env}.log")
end
def self.dispatch(args)
namespace, name = args[0].split(".")
subscriber = subscribers[namespace.to_sym]
@ -66,7 +73,10 @@ module Rails
subscriber.send(name, ActiveSupport::Notifications::Event.new(*args))
end
flush_all! if args[0] == "action_dispatch.callback"
if args[0] == "action_dispatch.callback" && !subscribers.empty?
flush_all!
log_tailer.tail! if tail_log
end
end
# Flush all subscribers' logger.

View file

@ -24,11 +24,13 @@ module SubscriberTest
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)
@ -103,6 +105,19 @@ module SubscriberTest
assert_equal 1, @logger.flush_count
end
def test_tails_logs_when_action_dispatch_callback_is_received
log_tailer = mock()
log_tailer.expects(:tail!)
Rails::Rack::LogTailer.expects(:new).with(nil, "log/development.log").returns(log_tailer)
Rails::Subscriber.tail_log = true
Rails::Subscriber.add :my_subscriber, @subscriber
instrument "action_dispatch.callback"
wait
ensure
Rails::Subscriber.tail_log = false
end
class SyncSubscriberTest < ActiveSupport::TestCase
include Rails::Subscriber::SyncTestHelper
include SubscriberTest