Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

puts vs logger in rails rake tasks

In a rake task if I use puts command then I see the output on console. However I will not see that message in log file when app is deployed on production.

However if I say Rails.logger.info then in development mode I see nothing on console. I need to go to log file and tail that.

I would ideally like to use Rails.logger.info and in development mode inside the rake task, the output from logger should also be sent to console.

Is there a way to achieve that?

like image 476
Nick Vanderbilt Avatar asked Feb 11 '10 17:02

Nick Vanderbilt


1 Answers

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.

like image 182
shmichael Avatar answered Sep 19 '22 01:09

shmichael