diff options
author | José Valim <jose.valim@gmail.com> | 2010-01-15 10:46:30 +0100 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2010-01-15 12:24:31 +0100 |
commit | b0994be5bd24559f1362a636b46271de6d6a92d6 (patch) | |
tree | e74234e7f473f8277e4393bd347f2ceab9dbe069 /railties | |
parent | b4019d5080181a911f8652cabd184794963911b6 (diff) | |
download | rails-b0994be5bd24559f1362a636b46271de6d6a92d6.tar.gz rails-b0994be5bd24559f1362a636b46271de6d6a92d6.tar.bz2 rails-b0994be5bd24559f1362a636b46271de6d6a92d6.zip |
LogTailer should be invoked after all logs in threads were flushed.
Diffstat (limited to 'railties')
-rw-r--r-- | railties/lib/rails/commands/server.rb | 6 | ||||
-rw-r--r-- | railties/lib/rails/rack/log_tailer.rb | 4 | ||||
-rw-r--r-- | railties/lib/rails/subscriber.rb | 14 | ||||
-rw-r--r-- | railties/test/subscriber_test.rb | 15 |
4 files changed, 30 insertions, 9 deletions
diff --git a/railties/lib/rails/commands/server.rb b/railties/lib/rails/commands/server.rb index b21ae2a17b..851a21c51d 100644 --- a/railties/lib/rails/commands/server.rb +++ b/railties/lib/rails/commands/server.rb @@ -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, diff --git a/railties/lib/rails/rack/log_tailer.rb b/railties/lib/rails/rack/log_tailer.rb index 077311be3c..3fa45156c3 100644 --- a/railties/lib/rails/rack/log_tailer.rb +++ b/railties/lib/rails/rack/log_tailer.rb @@ -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 diff --git a/railties/lib/rails/subscriber.rb b/railties/lib/rails/subscriber.rb index 88a6f77eaa..2c62f178b8 100644 --- a/railties/lib/rails/subscriber.rb +++ b/railties/lib/rails/subscriber.rb @@ -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. diff --git a/railties/test/subscriber_test.rb b/railties/test/subscriber_test.rb index 7e91b7333f..c45a68602d 100644 --- a/railties/test/subscriber_test.rb +++ b/railties/test/subscriber_test.rb @@ -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 |