Skip to content

Instantly share code, notes, and snippets.

@thecancerus
Created October 2, 2010 11:07
Show Gist options
  • Save thecancerus/607550 to your computer and use it in GitHub Desktop.
Save thecancerus/607550 to your computer and use it in GitHub Desktop.
<?php
class ExceptionHook
{
public function SetExceptionHandler()
{
set_exception_handler(array($this, 'HandleExceptions'));
}
public function HandleExceptions($exception)
{
$msg ='Exception of type \''.get_class($exception).'\' occurred with Message: '.$exception->getMessage().' in File '.$exception->getFile().' at Line '.$exception->getLine();
$msg .="\r\n Backtrace \r\n";
$msg .=$exception->getTraceAsString();
log_message('error', $msg, TRUE);
mail('[email protected]', 'An Exception Occurred', $msg, 'From: [email protected]');
}
<?php
class MY_Exceptions extends CI_Exceptions {
public function __construct()
{
parent::CI_Exceptions();
}
public 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);
}
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();
$msg = 'Severity: '.$severity.' --> '.$message. ' '.$filepath.' '.$line;
log_message('error', $msg , TRUE);
mail('[email protected]', 'An Error Occurred', $msg, 'From: [email protected]');
}
}
@lomse
Copy link

lomse commented Jun 22, 2017

Hi,Thanks for the library.
Im having an issue and Im hoping you could help.
I'm using Codeigniter 3.

application/config/hooks.php

$hook['pre_controller'][] = array(
    'class' => 'MY_Exceptions',
    'function' => 'log_exception',
    'filename' => 'MY_Exceptions.php',
    'filepath' => 'libraries'
);

In application/libraries/MY_Exceptions.php

class MY_Exceptions extends CI_Exceptions {

    function __construct() {
        parent::__construct();
    }

    function log_exception($severity, $message, $filepath, $line) {
        echo 'handling errors.';
    }

}

I keep getting Class 'CI_Exceptions' not found. Thoughts?

@moyed
Copy link

moyed commented Dec 24, 2018

You need to set
$config['subclass_prefix'] = 'MY_'; in config.php file

@charlie-eth
Copy link

Did you ever find the solution to this @lomse
I have $config['subclass_prefix'] = 'MY_';

but still getting Class 'CI_Exceptions' not found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment