简体   繁体   English

保存Whoops PrettyPageHandler的结果

[英]Save the result of Whoops PrettyPageHandler

I'm using the Whoops error library (and loving it) on dev to show the PrettyPageHandler like so: 我在dev上使用Whoops错误库(并且喜欢它)来显示PrettyPageHandler,如下所示:

if (ENVIRONMENT == 'local') {
    $whoops = new \Whoops\Run;
    $whoops->pushHandler(new \Whoops\Handler\PrettyPageHandler);
    $whoops->register();
}

For live I'm using the CallbackHandler to show the end user a "user friendly" error message. 对于直播我正在使用CallbackHandler向最终用户显示“用户友好”错误消息。

Is there anyway to save the result/output of PrettyPageHandler into a database or even to the filesystem? 无论如何将PrettyPageHandler的结果/输出保存到数据库甚至文件系统中? My thinking is to show the end user the friendly error page but record the error at the same time using the PrettyPageHandler to look back at and debug the error the user got. 我的想法是向最终用户显示友好的错误页面,但同时使用PrettyPageHandler记录错误以回顾并调试用户得到的错误。

Like described in the documentation: 如文档中所述:

$run->pushHandler(function($exception, $inspector, $run) {
    var_dump($exception->getMessage());
    return Handler::DONE;
});

replace var_dump($exception->getMessage()); 替换var_dump($exception->getMessage()); with your custom code to save to database or file log. 使用您的自定义代码保存到数据库或文件日志。

https://github.com/filp/whoops/blob/master/docs/API%20Documentation.md#core-handlers-1 https://github.com/filp/whoops/blob/master/docs/API%20Documentation.md#core-handlers-1

Edit1: EDIT1:

To use the PrettyPageHandler to save the log, create a custom handler extending from PrettyPageHandler, and in the place where the handle return the formated response before return the response to the user, save in database or filesytem in desired format. 要使用PrettyPageHandler保存日志,请创建一个从PrettyPageHandler扩展的自定义处理程序,并在句柄返回格式化响应的位置,然后将响应返回给用户,以所需格式保存在数据库或文件系统中。

You can do something like this: 你可以这样做:

<?php

class PrettyErrorLogger extends \Whoops\Handler\PrettyPageHandler
{
    public function handle()
    {
        parent::handle();
        $output = ob_get_clean();

        file_put_contents('error.log', $output); // or replace with DB insert
    }
}

then use it as handler 然后用它作为处理程序

$whoops->pushHandler(new PrettyErrorLogger);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM