柳暗花明笔记

    浅谈php 错误处理

    所属分类:[ php&web ] 猫哲学家 发表于:2015-05-06 18:08 浏览:589

    /**
    * 总结,通常register_shutdown_function和set_error_handler结合来用比较好
    * register_shutdown_function专门取Fatal错误,这里set_error_handler取不到(example $e = new aa)
    * 最后总结register_shutdown_function强于set_error_handler
    * I can use set_error_handler() to catch most PHP errors, but it doesn't work for fatal (E\_ERROR) errors, such as calling a function that doesn't exist. Is there another way to catch these errors?
    * Nope, that's just a limitation of set_error_handler(); it doesn't handle all errors.
    * The following error types cannot be handled with a user defined function: E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, and most of E_STRICT raised in the file where set_error_handler() is called.The register_shutdown_function() and error_get_last() is a decent workaround.
    */


    ini_set("display_errors", 0);
    ini_set('error_log','/home/wwwroot/test/php_error_test/log');
    // ini_set("display_errors", 0);

    // error_reporting(E_ALL & ~(E_NOTICE|E_DEPRECATED|E_STRICT));
    // set_error_handler(array('myerrors', 'my_set_error_handler'));
    // register_shutdown_function(array('myerrors', 'my_register_shutdown_function'));
    // set_error_handler(array('myerrors', 'my_set_error_handler'));
    // set_exception_handler(array('myerrors', 'my_set_exception_handler'));

    // register_shutdown_function(array('myerrors', 'my_register_shutdown_function'));

    require 's';

    // $e = new ww;


    /*try {
    // $e = new ww;
    require 's';
    } catch (Exception $e) {
    echo 444;
    myerrors::log($e->getMessage());
    }*/

    class myerrors
    {
    static public function log($mess = '')
    {
    // Log文件是绝对路径
    error_log(date('[Y-m-d H:i:s]') . ' ' . print_r($mess, 1) . PHP_EOL, 3, '/home/wwwroot/test/php_error_test/log');
    }

    static public function my_set_error_handler($level, $string, $file, $line)
    {
    // Message format
    $mess = "$level|$string in $file on line $line";
    self::log("set_error_handler: $mess");

    // 这一行不可少,如果少了会写两次log
    throw new Exception($mess);
    }

    static public function my_register_shutdown_function()
    {
    if ($error = error_get_last())
    {
    self::log("register_shutdown_function: {$error['type']}|{$error['message']} in {$error['file']} on line {$error['line']}");
    }
    }

    static public function my_set_exception_handler()
    {
    self::log("set_exception_handler: ");
    }
    }

    标签: php 
    上一编文章 Cannot find FreeTDS in known installation directories 解决办法 下一编文章 分享一个优秀的php开源框架
    最近文章
    发表评论