views:

196

answers:

2

In order to keep things DRY and share functionality between my Rails tasks and my "app", I put a lot of functionality in models. In my models I have "logger.info ..." but this is problematic when I call the model functions from rake tasks because nothing is logged. Is it possible to redirect logger for only rake tasks but have it log normally when the functionality is invoked by my "app" (web requests)?

Another related issue is I don't see ActiveMailer stuff logged when I send emails from rake tasks.

+1  A: 

You don't see logging, because a rake task is normally very short-lived. Logger, by default, only writes log to disk every 1000 characters. So your logs are less than 1000 characters and your rake task ends, nothing is written to disk.

The solution is to call

logger.flush

to force a write to disk before your rake task ends, preferably at the end of a model action.

Ariejan
A: 

I think the best way is to create a model log. I wrote an article about how to do it here

Tony
Well it'd mean you'll have two different logs. One in the *.log file and one in database. Not very easy for debugging. I'd really prefer writing to the log file.
Damien MATHIEU