Welcome Guest, Not a member yet? Register   Sign In
Log SQL Queries
#1

[eluser]Unknown[/eluser]
Is there a way to log all sql queries? I see that database sql errors are logged but is there a way to log all queries without modifying the core DB_Driver file?
#2

[eluser]centurionas[/eluser]
If you run CI on dedicated host you can enable the general mysql query log. Check the manual http://dev.mysql.com/doc/refman/5.1/en/query-log.html
#3

[eluser]pickupman[/eluser]
How about the output class.
Code:
$this->output->enable_profiler(TRUE);

CI already does this. You can view the code in the method show above. The code will append to the bottom of the page execution time, memory usage, POST, config, and queries. The profiler access a property from the DB class to get all executed queries.

The method actually calls _compile_queries() method from the Profiler class.
#4

[eluser]Unknown[/eluser]
Use this code
first enable hook on application/config/config.php file:
$config['enable_hooks'] = TRUE;
then
Code:
/* config/hooks.php */
$hook['post_system'][] = array(
        'class' => 'QueryLogHook',
        'function' => 'log_queries',
        'filename' => 'QueryLogHook.php',
        'filepath' => 'hooks'

/* application/hooks/QueryLogHook.php */
class QueryLogHook {

    function log_queries() {  
        $CI =& get_instance();
        $times = $CI->db->query_times;
        $dbs    = array();
        $output = NULL;    
        $queries = $CI->db->queries;

        if (count($queries) == 0)
        {
            $output .= "no queries\n";
        }
        else
        {
            foreach ($queries as $key=>$query)
            {
                $output .= $query . "\n";
            }
            $took = round(doubleval($times[$key]), 3);
            $output .= "===[took:{$took}]\n\n";
        }

        $CI->load->helper('file');
        if ( ! write_file(APPPATH  . "/logs/queries.log.txt", $output, 'a+'))
        {
             log_message('debug','Unable to write query the file');
        }  
    }

}




Theme © iAndrew 2016 - Forum software by © MyBB