From 59982acd632dbfa2b41818b35f81ca65731b5519 Mon Sep 17 00:00:00 2001 From: Jason Rudolph Date: Wed, 4 May 2011 18:46:24 -0400 Subject: Always flush logger at exit Prior to this change, running code via script/runner would demonstrate different logging behavior than running the same code via a rake task. In production mode the script/runner approach would always flush the logger, but the rake-based approach would not automatically flush the logger. This discrepancy violates the principle of least surprise, and it could lead to the loss of important production logging data. This change removes special-case code in the "runner" command, and replaces it with a general solution to ensure that the logger gets flushed at exit. This solution works for "runner", "console", "server", rake tasks, and any other process that loads the Rails environment. --- railties/lib/rails/application/bootstrap.rb | 1 + railties/lib/rails/commands/runner.rb | 22 ++++++++-------------- 2 files changed, 9 insertions(+), 14 deletions(-) (limited to 'railties') diff --git a/railties/lib/rails/application/bootstrap.rb b/railties/lib/rails/application/bootstrap.rb index 9c9d85eed6..0c02e5758e 100644 --- a/railties/lib/rails/application/bootstrap.rb +++ b/railties/lib/rails/application/bootstrap.rb @@ -37,6 +37,7 @@ module Rails ) logger end + at_exit { Rails.logger.flush if Rails.logger.respond_to?(:flush) } end # Initialize cache early in the stack so railties can make use of it. diff --git a/railties/lib/rails/commands/runner.rb b/railties/lib/rails/commands/runner.rb index 1a91d477ec..ddd08a32ee 100644 --- a/railties/lib/rails/commands/runner.rb +++ b/railties/lib/rails/commands/runner.rb @@ -39,18 +39,12 @@ ENV["RAILS_ENV"] = options[:environment] require APP_PATH Rails.application.require_environment! -begin - if code_or_file.nil? - $stderr.puts "Run '#{$0} -h' for help." - exit 1 - elsif File.exist?(code_or_file) - $0 = code_or_file - eval(File.read(code_or_file), nil, code_or_file) - else - eval(code_or_file) - end -ensure - if defined? Rails - Rails.logger.flush if Rails.logger.respond_to?(:flush) - end +if code_or_file.nil? + $stderr.puts "Run '#{$0} -h' for help." + exit 1 +elsif File.exist?(code_or_file) + $0 = code_or_file + eval(File.read(code_or_file), nil, code_or_file) +else + eval(code_or_file) end -- cgit v1.2.3