在Monolog ErrorHandler中设置最低PHP错误报告 [英] Set minimum PHP error reporting in Monolog ErrorHandler
问题描述
刚开始使用Monolog在PHP项目中记录错误,但是我想将最小错误报告设置设置为注意和上述。代码Im现在使用
Just started using Monolog to log errors in my PHP project but I want to set the minimum error reporting setting to NOTICE and above. The code Im using right now
use Monolog\ErrorHandler;
$handler = new ErrorHandler($logger);
$handler->registerErrorHandler([], false);
$handler->registerExceptionHandler();
$handler->registerFatalHandler();
其中产生所有错误,包括NOTICES。如何设置等价于
Which generates all errors including NOTICES. How can I set the equvalent to
error_reporting(E_ALL&〜E_NOTICE);
error_reporting(E_ALL & ~E_NOTICE);
使用Monolog
推荐答案
ErrorHandler
将捕获所有级别的 trigger_error
,除非您使用 @
操作符或实际设置error_reporting, error_reporting(E_ALL&〜E_NOTICE);
The ErrorHandler
will catch all levels of trigger_error
unless you silence them by using the @
operator or actually setting your error_reporting by error_reporting(E_ALL & ~E_NOTICE);
如果由于某种原因想要保留错误报告,同时过滤掉哪些错误被单声道错误处理程序捕获,我会继续扩展错误处理程序并注册。这样的东西:
If for some reason you want to keep error reporting but at the same time filter out which of those errors are caught by the monolog error handler, I'd go with extending the error handler and registering that instead. Something like this:
class MyErrorHandler extends Monolog\ErrorHandler{
public function handleError($code, $message, $file = '', $line = 0, $context = [])
{
if($code === E_NOTICE){
return;
}
parent::handleError($code, $message, $file, $line, $context);
}
}
use MyErrorHandler as ErrorHandler;
$handler = new ErrorHandler($logger);
$handler->registerErrorHandler([], false);
$handler->registerExceptionHandler();
$handler->registerFatalHandler();
请注意,我没有实际测试过这个,但我发现没有理由不会工作
Please note that I haven't actually tested this, but I find no reason it won't work
这篇关于在Monolog ErrorHandler中设置最低PHP错误报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!