Skip to content

Instantly share code, notes, and snippets.

@mehlah
Created November 10, 2012 10:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save mehlah/4050668 to your computer and use it in GitHub Desktop.
Save mehlah/4050668 to your computer and use it in GitHub Desktop.
Li3 database queries logger
<?php
use lithium\analysis\Logger;
use lithium\data\Connections;
use lithium\core\Environment;
use lithium\action\Dispatcher;
/**
* This filter intercepts the `run()` method of the `Dispatcher`
* and apply filters to `read()` method of the `Connection` to log in a file,
* all the queries passed to the database.
*
* @see lithium\core\Environment
* @see lithium\data\Connections
* @see lithium\analysis\Logger
*/
Logger::config(array(
'default' => array(
'production' => array(
'adapter' => 'File',
'file' => function($data, $config) { return "{$data['priority']}.prod.log"; }
)
)));
Dispatcher::applyFilter('run', function($self, $params, $chain) {
if (Environment::is('production')) {
Connections::get('default')->applyFilter('read', function($self, $params, $chain) {
$query = $params['query'];
Logger::debug('Read: ' . $query->source() . ': ' . json_encode($query->conditions()));
return $chain->next($self, $params, $chain);
});
}
return $chain->next($self, $params, $chain);
});
?>
@nilamdoc
Copy link

I tried putting this file in
/lithium/analysis/QueriesLogger.php

and included in my ImportController.php
use lithium\analysis\QueriesLogger;

It did not show any output on the page.

@mehlah
Copy link
Author

mehlah commented Nov 10, 2012

You should put this code in a file in config/bootstrap directory of your app, then load it in config/bootstrap.php by requiring it (just like other bootstrap files). It will load those filters during Lithium bootsrap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment