您的位置:首页 > 其它

***CI异常记录到日志:CodeIgniter中设计一个全局exception hook

2015-06-23 17:58 471 查看
在CodeIgniter中,当发生异常时,经常要通知系统管理员,因此有必要在全局的高度上
捕捉异常,因此可以写一个hook,
比如在config目录的hook.php中,加入:

$hook['pre_controller'][] = array(
'class'    => 'ExceptionHook',
'function' => 'SetExceptionHandler',
'filename' => 'ExceptionHook.php',
'filepath' => 'hooks'
);


然后在应用的hook目录下,编写ExceptionHook.php

<?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('dev-mail@example.com', 'An Exception Occurred', $msg, 'From: test@example.com');

}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: