zoukankan      html  css  js  c++  java
  • PHP set_exception_handler 设置异常处理函数

    If you're handling sensitive data and you don't want exceptions logging details such as variable contents when you throw them, you may find yourself frustratedly looking for the bits and pieces that make up a normal stack trace output, so you can retain its legibility but just alter a few things. In that case, this may help you:

    <?php

    function exceptionHandler($exception) {

        // these are our templates
        $traceline "#%s %s(%s): %s(%s)";
        $msg "PHP Fatal error:  Uncaught exception '%s' with message '%s' in %s:%s Stack trace: %s   thrown in %s on line %s";

        // alter your trace as you please, here
        $trace $exception->getTrace();
        foreach ($trace as $key => $stackPoint) {
            // I'm converting arguments to their type
            // (prevents passwords from ever getting logged as anything other than 'string')
            $trace[$key]['args'] = array_map('gettype'$trace[$key]['args']);
        }

        // build your tracelines
        $result = array();
        foreach ($trace as $key => $stackPoint) {
            $result[] = sprintf(
                $traceline,
                $key,
                $stackPoint['file'],
                $stackPoint['line'],
                $stackPoint['function'],
                implode(', '$stackPoint['args'])
            );
        }
        // trace always ends with {main}
        $result[] = '#' . ++$key ' {main}';

        // write tracelines into main template
        $msg sprintf(
            $msg,
            get_class($exception),
            $exception->getMessage(),
            $exception->getFile(),
            $exception->getLine(),
            implode(" "$result),
            $exception->getFile(),
            $exception->getLine()
        );

        // log or echo as you please
        error_log($msg);
    }

    ?>

    If you're not a fan of sprintf() or the duplicate $exception->getFile() and $exception->getLine() calls you can of course replace that as you like - consider this a mere compilation of the parts.

  • 相关阅读:
    在Fedora 8 下安装Fcitx输入法
    iisweb服务器完美解决方案
    利用httpget实现计划任务访问某一指定页面
    如何升级到SQL Server 2005
    排除“计算机默认 权限设置未将 COM 服务器应用程序”的错误
    Windows 2003 服务器安全设置
    Linux Fedora8 下安装 IE6
    配置 SQL Server 以便使用 2 GB 以上的物理内存(包括SQL Server 2005)
    Entity Framework初探
    async、await在ASP.NET[ MVC]中之线程死锁的故事
  • 原文地址:https://www.cnblogs.com/kenshinobiy/p/4598076.html
Copyright © 2011-2022 走看看