我试图在Exception Handler类中获取详细级别,以便我可以打印更多或更少的信息。
我正在编写一个运行数十万个循环的批处理脚本,因此我的目标是限制堆栈跟踪的详细程度,因为在这种情况下,唯一有用的跟踪是最后3或4。
我已经尝试了SO此处给出的其他答案,并且已经阅读了Internet的全部内容,但是没有人谈论在异常处理程序内部进行工作。
看起来更近的是this answer,它声明了$this->getOutput()->getVerbosity();
,但在错误处理程序中不起作用。
这是我的代码:
<?php
namespace App\Exceptions;
use Exception;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Mail;
use Symfony\Component\Debug\Exception\FlattenException;
use Symfony\Component\Debug\ExceptionHandler as SymfonyExceptionHandler;
use App\Mail\ExceptionOccured;
use Illuminate\Support\Facades\Log;
use Symfony\Component\Console\Output\OutputInterface;
class Handler extends ExceptionHandler
...
...
public function render($request, Exception $e)
{
// return parent::render($request, $e);
Log::error($this->buildMessage($e));
}
public function buildMessage($e)
{
$verbosity_level = $this->getOutput()->getVerbosity();
$is_verbose = ($verbosity_level >= OutputInterface::VERBOSITY_DEBUG);
$t0 = $e->getTrace()[0];
$tm = "Error: [{$e->getCode()}] {$e->getMessage()} @ {$t0['file']}:{$t0['line']}".PHP_EOL;
// if ($is_verbose){
...(more code) ...
// }
return $tm;
}
}
这是输出:
PHP Fatal error:
Uncaught Error: Call to undefined method App\Exceptions\Handler::getOutput() in ...\Handler.php:89
我已经看到output
属性存在,但是它受到保护并且没有吸气剂。
也许引用的答案对早期版本有效。
我真的迷路了,因为我只有2个月的时间与Laravel一起玩。
答案 0 :(得分:0)
异常处理程序为控制台提供了一种特定的方法:renderForConsole()
。
/**
* Render an exception to the console.
*
* @param \Symfony\Component\Console\Output\OutputInterface $output
* @param \Exception $e
* @return void
*/
public function renderForConsole($output, Exception $e)
{
(new ConsoleApplication)->renderException($e, $output);
}
它是Symfony Console组件的默认异常处理程序的非常简单的包装。定制您的需求!