【问题标题】:Using monolog and whoops使用独白和呐喊
【发布时间】:2019-12-24 19:54:56
【问题描述】:

我正在尝试在 set_exception_handlerset_error_handler 中使用 Monolog,而在外面哎呀。

我尝试了 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

【问题讨论】:

    标签: php exception monolog whoops


    【解决方案1】:

    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;       
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-30
      • 1970-01-01
      • 2019-07-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多