Hi guys,
I ‘m currently asking myself about the reverse issue. I want to log DB errors through CI Exceptions to get them logged everytime, even with $db[‘mydbconfigset’][‘db_debug’] = FALSE;
in the configuration file.
I’ve already extended the CI_Exceptions class to get reports on fatal errors via email. I like to keep notices logged but I think I’ll put them in a separate file or something like.
I tackled the DB_Driver problem to extend it without modifying core file, and on option I found was to leave the db_debug param on and handle real debugging and production logging by modifying the show_error. It’s a bit weird as a solution, but I’ll give it a try.
Here’s my quick& dirt extension to handle
<?php
/**
* Custom error handling
*/
class MY_Exceptions extends CI_Exceptions {
private $ci;
private $ignore_duplicates;
private $last_php_error_message;
private $last_db_error_message;
private $report_from_email;
private $report_to_email;
function MY_Exceptions() {
parent::CI_Exceptions();
$this->ignore_duplicates = TRUE;
$this->last_php_error_message = '';
$this->last_db_error_message = '';
$this->ci = &get;_instance();
$this->ci->load->library('email');
$this->report_from_email = $this->ci->config->item('error_report_to_email');
$this->report_to_email = $this->ci->config->item('error_report_from_email');
}
function __construct() {
$this->MY_Exceptions();
}
function log_exception($severity, $message, $filepath, $line) {
$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
$complete_message = 'Severity: '.$severity.' --> '.$message. ' '.$filepath;
log_message('error', $complete_message .' Filename:'.$line, TRUE);
if ($this->ignore_duplicates && $this->last_php_error_message == $complete_message) {
return;
}
$subject = 'PHP error report';
$body = $complete_message . "\nOn line : " . $line;
$this->report_email($subject, $body);
//ignore line number for error duplication check
$this->last_php_error_message = $complete_message;
}
/**
* General Error Page
*
* This function takes an error message as input
* (either as a string or an array) and displays
* it using the specified template.
*
* @access private
* @param string the heading
* @param string the message
* @param string the template name
* @return string
*/
function show_error($heading, $message, $template = 'error_general')
{
//report only db errors
if ($template == 'error_db') {
$email_message = $heading . "\n\n" . implode("\n", ( ! is_array($message)) ? array($message) : $message);
if ($this->ignore_duplicates && $this->last_db_error_message == $email_message) {
return parent::show_error($heading, $message, $template);
}
$this->report_email('DB error report', $email_message);
$this->last_db_error_message == $email_message;
}
return parent::show_error($heading, $message, $template);
}
private function report_email($subject, $message) {
$this->ci->email->from($this->report_from_email, 'CI errors');
$this->ci->email->to($this->report_to_email);
$this->ci->email->subject($subject);
$this->ci->email->message($message);
return $this->ci->email->send();
}
}
?>
I need to add some other stuff to disable email reporting while in developement mode and hide default CI db error with a nice excuse page while in production.
tdktank59, I think you can record php errors in a DB table the same way
Best regards,
Ben