puts vs logger in rails rake tasks
Put this in application.rb
, or in a rake task initialize code
if defined?(Rails) && (Rails.env == 'development') Rails.logger = Logger.new(STDOUT)end
This is Rails 3 code. Note that this will override logging to development.log
. If you want both STDOUT
and development.log
you'll need a wrapper function.
If you'd like this behaviour only in the Rails console, place the same block of code in your ~/.irbrc
.
You could create a new rake task to get this to work.
desc "switch logger to stdout"task :to_stdout => [:environment] do Rails.logger = Logger.new(STDOUT)end
This way when you execute your rake task you can add to_stdout first to get stdout log messages or don't include it to have messages sent to the default log file
rake to_stdout some_task
Rake tasks are run by a user, on a command-line. Anything they need to know right away ("processed 5 rows") should be output on the terminal with puts
.
Anything that needs to be kept for posterity ("sent warning email to jsmith@example.com") should be sent to the Rails.logger
.