美文网首页
[PHP错误异常]①⑤--观察者模式处理异常信息

[PHP错误异常]①⑤--观察者模式处理异常信息

作者: 子木同 | 来源:发表于2017-09-15 15:09 被阅读8次

    Exception_Observer.php

    <?php
    /**
     * 给观察者定义的规范
     * Interface Exception_observer
     */
    interface Exception_Observer
    {
        public function update(Observable_Exception $e);
    }
    

    Logging_Exception_Observer.php

    <?php
    
    class Logging_Exception_Observer implements Exception_Observer
    {
        protected $_filename = "D:/error/imoocException.log";
    
        function __construct($filename = null)
        {
            if ($filename != null && is_string($filename)) {
                $this->_filename = $filename;
            }
        }
    
        /**
         * 记录日志
         * @param Observable_Exception $e
         */
        public function update(Observable_Exception $e)
        {
            $message = "时间:" . date("Y-m-d H:i:s") . PHP_EOL;
            $message .= "信息:" . $e->getMessage() . PHP_EOL;
            $message .= "追踪信息:" . $e->getTraceAsString() . PHP_EOL;
            $message .= "文件:" . $e->getFile() . PHP_EOL;
            $message .= "行号:" . $e->getLine() . PHP_EOL;
            error_log($message, 3, $this->_filename);
        }
    }
    

    Emailling_Exception_Observer.php

    <?php
    
    class Emailling_Exception_Observer implements Exception_Observer
    {
        protected $_email = '403133112@qq.com';
    
        public function __construct($email = null)
        {
            if ($email != null && filter_var($email, FILTER_VALIDATE_EMAIL)) {
                $this->_email = $email;
            }
        }
    
        /**
         * 发送邮件
         * @param Observable_Exception $e
         */
        public function update(Observable_Exception $e)
        {
            $message = "时间:" . date("Y-m-d H:i:s") . PHP_EOL;
            $message .= "信息:" . $e->getMessage() . PHP_EOL;
            $message .= "追踪信息:" . $e->getTraceAsString() . PHP_EOL;
            $message .= "文件:" . $e->getFile() . PHP_EOL;
            $message .= "行号:" . $e->getLine() . PHP_EOL;
            error_log($message, 1, $this->_email);
        }
    }
    

    test.php

    <?php
    require_once 'Exception_observer.php';
    require_once 'Observable_Exception.php';
    require_once 'Logging_Exception_Observer.php';
    require_once 'Emailling_Exception_Observer.php';
    
    Observable_Exception::attach(new Logging_Exception_Observer());
    Observable_Exception::attach(new Logging_Exception_Observer('D:/error/test1.log'));
    //Observable_Exception::attach(new Emailling_Exception_Observer());
    
    class MyException extends Observable_Exception
    {
    
        public function test()
        {
            echo "this is a test";
        }
    
        public function test1()
        {
            echo "我是自定义的方法处理这个异常";
        }
    }
    
    try {
        throw new MyException('出现异常了,记录一下下!!!');
    } catch (MyException $e) {
        echo $e->getMessage();
        echo '<hr/>';
        $e->test();
        echo "<hr/>";
        $e->test1();
    }
    
    Paste_Image.png Paste_Image.png Paste_Image.png

    相关文章

      网友评论

          本文标题:[PHP错误异常]①⑤--观察者模式处理异常信息

          本文链接:https://www.haomeiwen.com/subject/zlsssxtx.html