Logging all requests after execution in codeigniter

I want to log all requests after execution with interceptors. -i allowed hooks in config.php is my hook →

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

- and this is detonation of the hook →

 class Db_log { function __construct() { } function logQueries() { $CI = & get_instance(); $filepath = APPPATH . 'logs/Query-log-' . date('Ym-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); } } 

- creating a query_log file - but no records of saved queries

+5
source share
2 answers

Your code looks good - the only reason this doesn't work is your database configuration - take a look at @your DB Connection in database.php in / config /

There is a save_queries option that must be set to true

 $db['default'] = array( ... 'save_queries' => TRUE ); 
+4
source

For me, this only worked when I defined the $ hook array in config / hooks.php.

0
source

All Articles