Add hook for database query logging - enable in config.php - (#924)

This commit is contained in:
FrancescoUK
2016-11-06 15:29:47 +00:00
parent 6dfb24e6f1
commit ef45c96ce8
5 changed files with 50 additions and 2 deletions

View File

@@ -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'] = '';

View File

@@ -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'
);

View File

@@ -0,0 +1,29 @@
<?php
// Name of function same as mentioned in Hooks Config
function db_log_queries()
{
$CI = & get_instance();
// check if database logging is enabled (see config/config.php)
if($CI->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);
}
}
?>

View File

@@ -54,3 +54,5 @@ function load_stats()
}
}
}
?>

View File

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