From acd2279e54bab2ed86afdc42b42fea1e98dbed48 Mon Sep 17 00:00:00 2001 From: mjollnir_ Date: Wed, 11 May 2005 23:34:46 +0000 Subject: [PATCH] Merged from MOODLE_14_STABLE: SQL errors are now (optionally -- defaults to off) logged to the apache error log. Helps debugging! Credit: Patrick Li --- config-dist.php | 5 +++++ lib/datalib.php | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) diff --git a/config-dist.php b/config-dist.php index 260ed6794cc..6b74d70a9c9 100644 --- a/config-dist.php +++ b/config-dist.php @@ -239,6 +239,11 @@ $CFG->admin = 'admin'; // then all non-teachers will always see these for every person. // $CFG->forcefirstname = 'Bruce'; // $CFG->forcelastname = 'Simpson'; +// +// The following setting will turn SQL Error logging on. This will output an +// entry in apache error log indicating the position of the error and the statement +// called. This option will action disregarding error_reporting setting. +// $CFG->dblogerror = true; //========================================================================= // ALL DONE! To continue installation, visit your main page with a browser diff --git a/lib/datalib.php b/lib/datalib.php index f83138df4eb..ec52b5bb1a0 100644 --- a/lib/datalib.php +++ b/lib/datalib.php @@ -65,6 +65,10 @@ function execute_sql($command, $feedback=true) { if ($feedback) { echo '

'. get_string('error') .'

'; } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $command"); + } return false; } } @@ -441,6 +445,10 @@ function record_exists_sql($sql) { if (isset($CFG->debug) and $CFG->debug > 7) { notify($db->ErrorMsg().'

'.$sql); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } return false; } @@ -524,6 +532,10 @@ function count_records_sql($sql) { if (isset($CFG->debug) and $CFG->debug > 7) { notify($db->ErrorMsg() .'

'. $sql); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } return 0; } @@ -592,6 +604,10 @@ function get_record_sql($sql, $expectmultiple=false) { if (isset($CFG->debug) and $CFG->debug > 7) { // Debugging mode - print checks notify( $db->ErrorMsg() . '

'. $sql . $limit ); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql$limit"); + } return false; } @@ -773,6 +789,10 @@ function get_records_sql($sql) { if (isset($CFG->debug) and $CFG->debug > 7) { notify($db->ErrorMsg() .'

'. $sql); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } return false; } @@ -878,6 +898,10 @@ function get_records_sql_menu($sql) { if (isset($CFG->debug) and $CFG->debug > 7) { notify($db->ErrorMsg() .'

'. $sql); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } return false; } @@ -928,6 +952,10 @@ function get_field($table, $return, $field1, $value1, $field2='', $value2='', $f if (isset($CFG->debug) and $CFG->debug > 7) { notify($db->ErrorMsg() .'

SELECT '. $return .' FROM '. $CFG->prefix . $table .' '. $select); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: SELECT $return FROM $CFG->prefix$table $select"); + } return false; } @@ -959,6 +987,10 @@ function get_field_sql($sql) { if (isset($CFG->debug) and $CFG->debug > 7) { notify($db->ErrorMsg() .'

'. $sql); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $sql"); + } return false; } @@ -1113,6 +1145,10 @@ function insert_record($table, $dataobject, $returnid=true, $primarykey='id') { if (isset($CFG->debug) and $CFG->debug > 7) { notify($db->ErrorMsg() .'

'.$insertSQL); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: $insertSQL"); + } return false; } @@ -1222,6 +1258,10 @@ function update_record($table, $dataobject) { if (isset($CFG->debug) and $CFG->debug > 7) { notify($db->ErrorMsg() .'

UPDATE '. $CFG->prefix . $table .' SET '. $update .' WHERE id = \''. $dataobject->id .'\''); } + if (!empty($CFG->dblogerror)) { + $debug=end(debug_backtrace()); + error_log("SQL ".$db->ErrorMsg()." in {$debug['file']} on line {$debug['line']}. STATEMENT: UPDATE $CFG->prefix$table SET $update WHERE id = '$dataobject->id'"); + } return false; } }