1

我正在尝试在set_exception_handlerand中使用 Monolog set_error_handler,而在外面使用 Whoops。

我尝试了 2 个运行 Whoops 的位置。

  1. Run Whoops #1 Location
    • 结果:Monolog 记录错误/异常并且 Whoops 不显示
  2. Run Whoops #2 Location
    • 结果:Monolog 没有记录任何内容,并且显示 Whoops

我被困在为什么我不能让 Monolog 和 Whoops 一起工作。

...
use Whoops\Run;
use Whoops\Handler\PrettyPageHandler;

error_reporting(E_ALL);
ini_set('display_errors', 1);
//Run Whoops #1 Location
$whoops = new Run();
$whoops->prependHandler(new PrettyPageHandler());
$whoops->register();

function exceptionHandler($e)
{ 
    //log the exception using monolog
}

function errorHandler($errno, $errstr, $errfile, $errline)
{
    //log the error using monolog
}

set_error_handler("errorHandler");
set_exception_handler('exceptionHandler');

//Run Whoops #2 Location
4

1 回答 1

0

php 中的set_exception_handler函数被 Whoops 库覆盖。我们可以使用 Whoops 库的pushHandler方法来注册一个通用处理函数,并在该函数内部初始化 Monolog:

use Monolog\Formatter\LineFormatter;
$whoops = new \Whoops\Run;
$whoops->pushHandler(function($exception, $inspector, $run) {

   // Initialise Monolog here. Example with stack trace support:
    $formatter = new LineFormatter(
        LineFormatter::SIMPLE_FORMAT, 
        LineFormatter::SIMPLE_DATE
    );
    $formatter->includeStacktraces(true);
    $errlog = new Logger('Error Log');
    $stream = new StreamHandler("logs/error.log", Logger::ERROR);
    $stream->setFormatter($formatter);
    $errlog->pushHandler($stream);
    $errlog->error($exception);

    return Handler::DONE;       
});
于 2020-04-14T03:13:21.143 回答