doutan3040 2015-03-08 16:00
浏览 34
已采纳

在生产服务器上格式化PHP错误

I have custom error handlers on my production computer:

register_shutdown_function( "check_for_fatal" );
set_error_handler('errHandle');
set_exception_handler( "log_exception" );

All errors are sent to mail. But format is horrible (straight line text).

Does anybody know any library, function, owns any code, that would format error in html with as many details as possible. (message, file, line, errorclass, complete callstack, perhaps global variables - $_GET, $_POST, ...) As much information as possible, so I will have easier job fixing issues.

  • 写回答

2条回答 默认 最新

  • dongxiusuo9881 2015-03-08 18:08
    关注

    I also make my own. Since I deal a lot with PDO, I also check if exception is PDO and add some details.

    Output is not pretty, but it is meant for developer and nobody else.

    static function FormatException($exception, $level = 0)
            {
                $result = "";
                $result = sprintf('<div style="padding-left:%dpx">', $level * 20);
                $result .= sprintf("<div><b>Message: </b> %s</div>", $exception->getMessage());
                $result .= sprintf("<div><b>Code: </b> %s</div>", $exception->getCode());
                $result .= sprintf("<div><b>File: </b> %s</div>", $exception->getFile());
                $result .= sprintf("<div><b>Line: </b> %s</div>", $exception->getLine());
                $result .= sprintf("<div><b>Trace: </b></div>");
                $result .= sprintf('<div style="padding-left: 20px;">');
                foreach ($exception->getTrace() as $trace)
                {
                    $result .= sprintf('<div style="border: 1px solid #000;margin-top: 10px;">');
                    $result .= sprintf("<div><b>File: </b> %s</div>", $trace['file']);
                    $result .= sprintf("<div><b>Line: </b> %s</div>", $trace['line']);
                    $result .= sprintf("<div><b>Function: </b> %s</div>", $trace['function']);
                    $result .= sprintf("<div><b>Class: </b> %s</div>", $trace['class']);
                    $result .= sprintf("<div><b>Type: </b> %s</div>", $trace['type']);
                    $result .= sprintf("<div><b>Args: </b></div>");
                    $result .= sprintf('<div style="padding-left: 20px;">');
                    foreach ($trace['args'] as $arg)
                        $result .= sprintf("<div><b>Arg: </b>%s</div>", json_encode($arg));
                    $result .= sprintf("</div>");
                    $result .= sprintf("</div>");
                }
                $result .= sprintf("</div>");
                if (get_class($exception) === "PDOException")
                {
                    $result .= sprintf("<br />");
                    $result .= sprintf("<div><b>PDO: </b></div>");
                    $result .= sprintf('<div style="padding-left: 20px;">');
                    $result .= sprintf("<div><b>Code: </b> %s</div>", $exception->errorInfo[0]);
                    $result .= sprintf("<div><b>Driver code: </b> %s</div>", $exception->errorInfo[1]);
                    $result .= sprintf("<div><b>Message: </b> %s</div>", $exception->errorInfo[2]);
                    $result .= sprintf("</div>");
                }
    
                $prev = $exception->getPrevious();
                if (isset($prev))
                    $result .= self::FormatException($prev, $level++);
                $result .= sprintf("</div>");
                $result .= sprintf("<br />");
    
                return $result;
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题