Home > php教程 > php手册 > body text

php自定义错误类,实现错误追踪功能

WBOY
Release: 2016-06-06 19:38:20
Original
1632 people have browsed it

php环境都可以使用,使用方法: require./myErrorHandler.php; //显示所有的错误 error_reporting(-1); ini_set(display_error,1); //调用错误类 set_error_handler(array(myErrorHandler,deal)); 无 ?php/** * Created by PhpStorm. * User: yangyulong * Da

php环境都可以使用, 使用方法:
require './myErrorHandler.php';
//显示所有的错误
error_reporting(-1);
ini_set('display_error', 1);
//调用错误类
set_error_handler(array('myErrorHandler', 'deal'));
<?php
/**
 * Created by PhpStorm.
 * User: yangyulong
 * Date: 2015/5/8
 * Time: 9:46
 */

class myErrorHandler {
    public $message='';
    public $filename='';
    public $line=0;
    public $vars=array();

    public function __construct($message, $filename, $line, $vars){
        $this->message = $message;
        $this->filename = $filename;
        $this->line = $line;
        $this->vars = $vars;
    }

    public static function deal($errno, $errmsg, $filename, $line, $vars){
        $self = new self($errmsg, $filename, $line, $vars);

        switch($errno){
            case E_USER_ERROR:
                return $self->dealError();
                break;
            case E_USER_WARNING:
            case E_WARNING:
                return $self->dealWarning();
                break;
            case E_USER_NOTICE:
            case E_NOTICE:
                return $self->dealNotice();
                break;
            default:
                return false;
        }
    }

    /**
     * 产生致命错误
     */
    public function dealError(){
        //开启内存缓存
        ob_start();
        debug_print_backtrace();
        //获取内存中的信息
        $backtrace = ob_get_flush();
        $errorMsg = "
            出现了致命的错误,如下:
            产生的错误文件:{$this->filename}<br />
            产生错误的信息:{$this->message}<br />
            禅僧错误的行号:{$this->line}<br />
            追踪信息:{$backtrace}<br />
            ==================================
        ";
        //发送邮件
        error_log($errorMsg, 3, 'D:\noitice\noticeLog.log');
        exit(1);
    }

    /**
     * 错误警告
     * @return bool
     */
    public function dealWarning(){
        $errorMsg = "
            出现了警告的错误,如下:
            产生的错误文件:{$this->filename}<br />
            产生错误的信息:{$this->message}<br />
            错误警告的行号:{$this->filename}:{$this->line}<br />
            ==================================
        ";
        //发送邮件
        return error_log($errorMsg, 3, 'D:\noitice\noticeLog.log');
    }

    /**
     * 定义通知级别的错误
     * @return bool
     */
    public function dealNotice(){
        $datetime = date('Y-m-d H:i:s', time());
        $errorMsg = "
            出现了通知的错误,如下:
            产生的错误文件:{$this->filename}<br />
            产生错误的信息:{$this->message}<br />
            禅僧错误的行号:{$this->filename}:{$this->line}<br />
            通知时间:{$datetime}
            ==================================
        ";
        //发送邮件
        return error_log($errorMsg, 3, 'D:\noitice\noticeLog.log');
    }
}
Copy after login
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template