Browse files

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.
  • Loading branch information...
1 parent cae68d2 commit 59982acd632dbfa2b41818b35f81ca65731b5519 @jasonrudolph jasonrudolph committed May 4, 2011
Showing with 9 additions and 14 deletions.
  1. +1 −0 railties/lib/rails/application/bootstrap.rb
  2. +8 −14 railties/lib/rails/commands/runner.rb
1 railties/lib/rails/application/bootstrap.rb
@@ -37,6 +37,7 @@ module Bootstrap
+ at_exit { Rails.logger.flush if Rails.logger.respond_to?(:flush) }
# Initialize cache early in the stack so railties can make use of it.
22 railties/lib/rails/commands/runner.rb
@@ -39,18 +39,12 @@
require APP_PATH
- 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(, nil, code_or_file)
- else
- eval(code_or_file)
- end
- 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(, nil, code_or_file)
+ eval(code_or_file)

0 comments on commit 59982ac

Please sign in to comment.