aboutsummaryrefslogtreecommitdiffstats
path: root/railties
diff options
context:
space:
mode:
authorJason Rudolph <github@jasonrudolph.com>2011-05-04 18:46:24 -0400
committerJason Rudolph <github@jasonrudolph.com>2011-05-06 12:21:21 -0400
commit59982acd632dbfa2b41818b35f81ca65731b5519 (patch)
tree15d027194b664fb64e5023dd6fbc34ff1247e305 /railties
parentcae68d221cc81232e30049eafa4ff59b2c5f6010 (diff)
downloadrails-59982acd632dbfa2b41818b35f81ca65731b5519.tar.gz
rails-59982acd632dbfa2b41818b35f81ca65731b5519.tar.bz2
rails-59982acd632dbfa2b41818b35f81ca65731b5519.zip
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.
Diffstat (limited to 'railties')
-rw-r--r--railties/lib/rails/application/bootstrap.rb1
-rw-r--r--railties/lib/rails/commands/runner.rb22
2 files changed, 9 insertions, 14 deletions
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