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

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?

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

How about the output class.

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.

Use this code
first enable hook on application/config/config.php file:
$config['enable_hooks'] = TRUE;
/* 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";
            foreach ($queries as $key=>$query)
                $output .= $query . "\n";
            $took = round(doubleval($times[$key]), 3);
            $output .= "===[took:{$took}]\n\n";

        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