To save a Log of all SQL (with established test it brings the SELECT
only).
What would it be like:
Go to your configuration file application/config/config.php
and the key enable_hooks put $config['enable_hooks'] = TRUE
;
Also within application/config
in file hooks.php
put the following code:
$hook['post_controller'] = array( // 'post_controller' indicated execution of hooks after controller is finished
'class' => 'Db_log', // Name of Class
'function' => 'logQueries', // Name of function to be executed in from Class
'filename' => 'db_log.php', // Name of the Hook file
'filepath' => 'hooks' // Name of folder where Hook file is stored
);
Now create a file in application/hooks
with the name of db_log.php
with the following code:
class Db_log {
function __construct()
{
}
function logQueries()
{
$CI = & get_instance();
$filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.php';
$handle = fopen($filepath, "a+");
$times = $CI->db->query_times;
foreach ($CI->db->queries as $key => $query)
{
$sql = $query . " \n Execution Time:" . $times[$key];
fwrite($handle, $sql . "\n\n");
}
fclose($handle);
}
}
Ready, shortly after the first run, a log > file will be generated by date, inside the application/logs
folder, storing all its SQL .
Website copyright and reference: JIGAR JAIN - Logging / Saving all DB queries in Codeigniter
There is also bitbucket for this settings: Codeigniter - Log all DB Queries in Log File , if you want to clone the repository.