Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

logging all queries after execution in codeigniter

Tags:

codeigniter

i want to log all queries after execution using hooks. -i enabled hooks in config.php -this is my hook-->

 $hook['post_controller'] = array(    
    'class' => 'Db_log',             
    'function' => 'logQueries',     
   'filename' => 'db_log.php',    
   'filepath' => 'hooks'         
  );

-and this is hook defination-->

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);  
    }

}

--its creating query_log file --but no records of queries being stored

like image 294
mohseen pathan Avatar asked Sep 20 '16 07:09

mohseen pathan


1 Answers

Your Code looks fine - the only reason why this doesn't work is in your DB Configuration - take a look @your DB Connection in the database.php under application/config/

There is an option "save_queries" which should be set to true

$db['default'] = array(
    ...
    'save_queries' => TRUE
);
like image 173
sintakonte Avatar answered Nov 17 '22 10:11

sintakonte