Skip to content

Instantly share code, notes, and snippets.

@khanhicetea
Created July 3, 2015 10:48
Show Gist options
  • Save khanhicetea/d7966f6775df9fe31ce8 to your computer and use it in GitHub Desktop.
Save khanhicetea/d7966f6775df9fe31ce8 to your computer and use it in GitHub Desktop.
SQLLog Silex
// 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