diff --git a/application/config/config.php b/application/config/config.php index ca8532eff..497375784 100644 --- a/application/config/config.php +++ b/application/config/config.php @@ -33,6 +33,16 @@ $config['commit_sha1'] = '$Id$'; */ $config['ospos_xss_clean'] = TRUE; +/* +|-------------------------------------------------------------------------- +| Enable database query logging hook +|-------------------------------------------------------------------------- +| +| Logs are stored in application/logs +| +*/ +$config['db_log_enabled'] = FALSE; + /* |-------------------------------------------------------------------------- | Base Site URL @@ -544,4 +554,4 @@ $config['rewrite_short_tags'] = FALSE; | Comma-separated: '10.0.1.200,192.168.5.0/24' | Array: array('10.0.1.200', '192.168.5.0/24') */ -$config['proxy_ips'] = ''; +$config['proxy_ips'] = ''; \ No newline at end of file diff --git a/application/config/hooks.php b/application/config/hooks.php index 504a81e18..736e1c00e 100644 --- a/application/config/hooks.php +++ b/application/config/hooks.php @@ -25,3 +25,10 @@ $hook['post_controller_constructor'][] = array( 'filepath' => 'hooks' ); +// 'post_controller' indicated execution of hooks after controller is finished +$hook['post_controller'] = array( + 'class' => '', + 'function' => 'db_log_queries', + 'filename' => 'db_log.php', + 'filepath' => 'hooks' + ); \ No newline at end of file diff --git a/application/hooks/db_log.php b/application/hooks/db_log.php new file mode 100644 index 000000000..81445547c --- /dev/null +++ b/application/hooks/db_log.php @@ -0,0 +1,29 @@ +config->item('db_log_enabled')) + { + // Creating Query Log file with today's date in application/logs folder + $filepath = APPPATH . 'logs/Query-log-' . date('Y-m-d') . '.log'; + // Opening file with pointer at the end of the file + $handle = fopen($filepath, "a+"); + + // Get execution time of all the queries executed by controller + $times = $CI->db->query_times; + foreach ($CI->db->queries as $key => $query) + { + // Generating SQL file alongwith execution time + $sql = $query . " \n Execution Time:" . $times[$key]; + // Writing it in the log file + fwrite($handle, $sql . "\n\n"); + } + + // Close the file + fclose($handle); + } +} +?> \ No newline at end of file diff --git a/application/hooks/load_stats.php b/application/hooks/load_stats.php index 1cb4c2170..e5c592a7d 100644 --- a/application/hooks/load_stats.php +++ b/application/hooks/load_stats.php @@ -54,3 +54,5 @@ function load_stats() } } } + +?> \ No newline at end of file diff --git a/application/models/Item.php b/application/models/Item.php index 3485e734f..1cfc03a79 100644 --- a/application/models/Item.php +++ b/application/models/Item.php @@ -9,7 +9,7 @@ class Item extends CI_Model if (ctype_digit($item_id)) { $this->db->from('items'); - $this->db->where('item_id', (int)$item_id); + $this->db->where('item_id', (int) $item_id); if ($ignore_deleted == FALSE) { $this->db->where('deleted', $deleted);