I am use this approach:
<?php
$config = new \Phalcon\Config([
'db' => [
'adapter' => 'Mysql',
'host' => 'localhost',
'dbname' => 'test',
'username' => 'root',
'password' => ''
],
'path' => [
'controllers' => __DIR__.'/../app/controllers/',
'libraries' => __DIR__.'/../app/libraries/',
'messages' => __DIR__.'/../app/messages/',
'models' => __DIR__.'/../app/models/',
'views' => __DIR__.'/../app/views/',
'tmp' => __DIR__.'/../tmp/',
],
]);
$di->set('db', function() use ($config) {
$adapter = '\Phalcon\Db\Adapter\Pdo\\'.$config->db->adapter;
$connection = new $adapter((array) $config->db);
$logger = new \Phalcon\Logger\Adapter\File($config->path->tmp.'sql.log');
$eventsManager = new \Phalcon\Events\Manager();
$eventsManager->attach('db', function($event, $connection) use ($logger) {
if ($event->getType() == 'beforeQuery') $logger->log($connection->getSQLStatement().' '.join(', ', $connection->getSQLVariables()));
});
$connection->setEventsManager($eventsManager);
return $connection;
});