I'd like Symfony to log the Doctrine SQL queries that one of my task开发者_C百科s executes to a log file, much like the web debug toolbar does for non-cli code. Is this possible?
- Symfony version: 1.4.12
- Doctrine version: 1.2.4
Here's some example code for a task. I would like the SELECT query to be logged in a similar way to how it would be if it was called from an action.
class exampleTask extends sfBaseTask
{
protected function configure()
{
parent::configure();
$this->namespace = 'test';
$this->name = 'example';
}
protected function execute($arguments = array(), $options = array())
{
$databaseManager = new sfDatabaseManager($this->configuration);
$users = Doctrine_Core::getTable('SfGuardUser')
->createQuery('s')
->select('s.first_name')
->execute();
foreach($users as $user) {
print $user->getFirstName()."\n";
}
}
}
Try using the global task option --trace
(shortcut -t
). Like:
./symfony --trace namespace:task
It logs database queries the moment they are executed.
Don't forget to enable logging in the settings.yml of the application you're running the task in.
So if you're running the task in the dev
environment of the backend you would edit apps/backend/config/settings.yml
:
dev:
.settings:
logging_enabled: true
Note that this also logs stack traces of exception which might also be very helpful if you need to debug your tasks.
If you turn logging on all queries should be logged to your application log in the log
directory. To do this set logging_enabled
to true
in your settings.yml
.
You can then tail -f
on the logfile and see what's going on.
The following link contains a nice tutorial on how to enable logging of the Doctrine queries. (translated from the original portuguese to english)
http://translate.google.com/translate?js=n&prev=_t&hl=en&ie=UTF-8&layout=2&eotf=1&sl=pt&tl=en&u=http%3A%2F%2Fwww.michaelpaul.com.br%2Flog-queries-doctrine.html&act=url
精彩评论