Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Can't log SQL queries when the standard db name is not used.

My app logs queries just fine when I use the standard way of query logging:

$di->setShared('db', function() use ($config){
        $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            "host"        => $config->database->host,
            "username"    => $config->database->username,
            "password"    => $config->database->password,
            "dbname"  => $config->database->name
        ));

            $eventsManager = new \Phalcon\Events\Manager();
            $logger = new \Phalcon\Logger\Adapter\File($config->application->logsDir . 'sql.log');          
            $eventsManager->attach('db', function($event, $connection) use ($logger){              
                if($event->getType() == 'beforeQuery'){
                    $logger->log($connection->getRealSQLStatement());
                }
            });

            $connection->setEventsManager($eventsManager);

        return $connection;
    });

However, when i rename the service:

    $di->setShared('another_db', function() use ($config){
        $connection = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
            "host"        => $config->database->host,
            "username"    => $config->database->username,
            "password"    => $config->database->password,
            "dbname"  => $config->database->name
        ));

            $eventsManager = new \Phalcon\Events\Manager();
            $logger = new \Phalcon\Logger\Adapter\File($config->application->logsDir . 'sql.log');          
            $eventsManager->attach('another_db', function($event, $connection) use ($logger){              
                if($event->getType() == 'beforeQuery'){
                    $logger->log($connection->getRealSQLStatement());
                }
            });

            $connection->setEventsManager($eventsManager);

        return $connection;
    });

It does not log sql statements.

Do I need to do something else to get logging to work? I use this in my models to attach to the service:

$this->setConnectionService('another_db');

Thanks.



6.3k
Accepted
answer

All database events are fired under db namespace, so you need to identify the connection

<?php

$di->setShared('db', function () use ($config, $di) {
    $connection   = new \Phalcon\Db\Adapter\Pdo\Mysql($config->database1->toArray());
    $connectionId = $connection->getConnectionId();

    $logger = new \Phalcon\Logger\Adapter\File($config->application->logsDir . 'sql1.log');          

    $di['eventsManager']->attach('db', function ($event, $connection) use ($connectionId, $logger) {
        if ($connection->getConnectionId() === $connectionId && $event->getType() === 'beforeQuery') {
            $logger->log($connection->getRealSQLStatement());
        }
    });

    $connection->setEventsManager($di['eventsManager']);

    return $connection;
});

$di->setShared('another_db', function () use ($config, $di) {
    $connection   = new \Phalcon\Db\Adapter\Pdo\Mysql($config->database2->toArray());
    $connectionId = $connection->getConnectionId();

    $logger = new \Phalcon\Logger\Adapter\File($config->application->logsDir . 'sql2.log');          

    $di['eventsManager']->attach('db', function ($event, $connection) use ($connectionId, $logger) {
        if ($connection->getConnectionId() === $connectionId && $event->getType() === 'beforeQuery') {
            $logger->log($connection->getRealSQLStatement());
        }
    });

    $connection->setEventsManager($di['eventsManager']);

    return $connection;
});

Thanks, that works.