17

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

2 Answers 2

15

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
);
Sign up to request clarification or add additional context in comments.

6 Comments

above code is now working fine...but its logging SELECT queries only,what if i want only INSERT,UPDATE and DELETE be logged instead of SELECT queries?
even the other types of Queries should get logged - in the queries are all type of queries
is there any option to "$CI->db->queries"
what do you mean with option ? do you want to separate inserts, updates, delets and selects from each other ?
internal redirection in application was causing it to overtake hook execution, that's why it wasn't logging modification statements...i found the solution thereafter.
|
0

For me it only worked when I define de $hook array in config/hooks.php .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.