[eluser]AtlantixMedia[/eluser]
I would like to hear from someone who has successfully implemented a custom error tracking system with db logging and custom 404 pages. what I'm trying to accomplish is:
1) Saving errors to a db table. I'm partially able to accomplish this by extending the exceptions class. please see below.
2) show a custom 404 page. I'm partially able to accomplish this by extending the router class. please see below. I say partially because this system seems to work when I input a totally fake URL. i.e.:
www.domain.com/nonexistent. however, if I do something like this:
www.domain.com/media/video/shownonexistent where
www.domain.com/media/video/show points to a real controller functions, CI shows the default 404 page.
I would like to start a discussion on how to effectively build a custom error tracking and 404 pages as I believe many others will benefit from it.
with regard to #1, is extending the exceptions class the proper way to approach this?
thanks in advance
MY_Exceptions
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class MY_Exceptions extends CI_Exceptions{
function MY_Exceptions(){
parent::CI_Exceptions();
}
// --------------------------------------------------------------------
/**
* 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')
{
$message = '<p>'.implode('</p><p>', ( ! is_array($message)) ? array($message) : $message).'</p>';
$CI = & get_instance();
if ($CI->config->item('EnableErrorLogging') == 1)
{
switch($template)
{
case 'error_general': $type = 'GEN'; break;
case 'error_404': $type = '404'; break;
case 'error_db': $type = 'DB'; break;
case 'error_php': $type = 'PHP'; break;
default: $type = 'GEN';
}
$CI->load->model('Admin_Model');
$CI->Admin_Model->LogError_Q($type, '', $message, '', '');
}
if ($CI->config->item('ShowErrors') == 1)
{
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include(APPPATH.'errors/'.$template.EXT);
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
}
// --------------------------------------------------------------------
/**
* Native PHP error handler
*
* @access private
* @param string the error severity
* @param string the error string
* @param string the error filepath
* @param string the error line number
* @return string
*/
function show_php_error($severity, $message, $filepath, $line)
{
$severity = ( ! isset($this->levels[$severity])) ? $severity : $this->levels[$severity];
$filepath = str_replace("\\", "/", $filepath);
// For safety reasons we do not show the full file path
if (FALSE !== strpos($filepath, '/'))
{
$x = explode('/', $filepath);
$filepath = $x[count($x)-2].'/'.end($x);
}
$CI = & get_instance();
if ($CI->config->item('EnableErrorLogging') == 1)
{
$CI->load->model('Admin_Model');
$CI->Admin_Model->LogError_Q('PHP', $severity, $message, $filepath, $line);
}
if ($CI->config->item('ShowErrors') == 1)
{
if (ob_get_level() > $this->ob_level + 1)
{
ob_end_flush();
}
ob_start();
include(APPPATH.'errors/error_php'.EXT);
$buffer = ob_get_contents();
ob_end_clean();
echo $buffer;
}
}
}
?>