Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging Doctrine queries from Symfony 1.4 tasks

I'd like Symfony to log the Doctrine SQL queries that one of my tasks 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";
                }
        }
}
like image 521
Ian Gregory Avatar asked Jul 16 '11 20:07

Ian Gregory


1 Answers

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.

like image 104
flu Avatar answered Nov 18 '22 12:11

flu