Created
July 3, 2015 10:48
-
-
Save khanhicetea/d7966f6775df9fe31ce8 to your computer and use it in GitHub Desktop.
SQLLog Silex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Source : https://groups.google.com/d/msg/silex-php/ceKqVPlwLg0/kPkcM5Hx5owJ | |
if ( $app['debug'] ) { | |
$logger = new Doctrine\DBAL\Logging\DebugStack(); | |
$app['db.config']->setSQLLogger($logger); | |
$app->error(function(\Exception $e, $code) use ($app, $logger) { | |
if ( $e instanceof PDOException and count($logger->queries) ) { | |
// We want to log the query as an ERROR for PDO exceptions! | |
$query = array_pop($logger->queries); | |
$app['monolog']->err($query['sql'], array('params' => | |
$query['params'], 'types' => $query['types'])); | |
} | |
}); | |
$app->after(function(Request $request, Response $response) use | |
($app, $logger) { | |
// Log all queries as DEBUG. | |
foreach ( $logger->queries as $query ) { | |
$app['monolog']->debug($query['sql'], array('params' => | |
$query['params'], 'types' => $query['types'])); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment