Permalink
Browse files

Direct logging of Active Record to STDOUT so it's shown inline with t…

…he results in the console [DHH]
  • Loading branch information...
dhh committed Mar 31, 2011
1 parent 6eff044 commit caf0a72c85ddd30f901c33a2d72cf2a7453ed469
Showing with 6 additions and 0 deletions.
  1. +2 −0 railties/CHANGELOG
  2. +4 −0 railties/lib/rails/commands/console.rb
View
@@ -1,5 +1,7 @@
*Rails 3.1.0 (unreleased)*
+* Direct logging of Active Record to STDOUT so it's shown inline with the results in the console [DHH]
+
* Added `config.force_ssl` configuration which loads Rack::SSL middleware and force all requests to be under HTTPS protocol [DHH, Prem Sichanugrist, and Josh Peek]
* Added `rails plugin new` command which generates rails plugin with gemspec, tests and dummy application for testing [Piotr Sarnacki]
@@ -34,6 +34,10 @@ def start
exit
end
end
+
+ if defined?(ActiveRecord)
+ ActiveRecord::Base.logger = Logger.new(STDOUT)
+ end
if options[:sandbox]
puts "Loading #{Rails.env} environment in sandbox (Rails #{Rails.version})"

5 comments on commit caf0a72

Contributor

ahoward replied Mar 31, 2011

logging to STDER seems more prudent - just in case someone tries to pipe some output into another command. also, it's never buffered.

In what context do you expect someone to pipe from ./script/rails console?

Contributor

ahoward replied Mar 31, 2011

so, i have this

https://gist.github.com/897413

a side effect of always logging is that sometimes you want to turn it off (note my ENV test). in the case that you log in STDERR you can simply do:

rails c 2>/dev/null

even better you can do

https://gist.github.com/897418

logging on STDOUT is simply being a poor unix citizen, but letting the user decide where the logging will go you can facilitate alot of neat unix tricks.

also, serious issues can arise with buffering and bad terminal settings. STDERR is not buffered and just this fact prevents a lot of headaches.

put another way - what good reason is there for ever mixing output with logging?

I don't disagree that STDERR would probably be more appropriate, but buffering is irrelevant in the context of an interactive console.

Owner

dhh replied Mar 31, 2011

Switched to STDERR:

ActiveRecord::Base.logger = Logger.new(STDERR)

Please sign in to comment.