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?
You could create a new rake task to get this to work.
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
Put this in
application.rb
, or in a rake task initialize codeThis is Rails 3 code. Note that this will override logging to
development.log
. If you want bothSTDOUT
anddevelopment.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
.I'd say that using
Rails.logger.info
is the way to go.You won't be able to see it in the server console because it won't run via the server. Just open up a new console and
tail -f
the log file, it'll do the trick.(via)
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
.In Rails 2.X to redirect the logger to STDOUT in models:
To redirect logger in controllers:
How about creating an application helper which detects which environment is running and does the right thing?
Then call output_debug instead of puts or logger.info