问题
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();
Which generates all errors including NOTICES. How can I set the equvalent to
error_reporting(E_ALL & ~E_NOTICE);
using Monolog
回答1:
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
回答2:
Monolog logging levels are unrelated to PHP logging levels. PHP logging levels as set by error_reporting()
control what types of things the PHP engine itself reports inside its own output. Monolog logging levels emulate unix syslog levels and are intended to be driven by the application, not by PHP. I.e., error_reporting()
handles messages generated by PHP, while Monolog handles messages generated by your app. For example, within your application, when you issue a log, you indicate its severity:
if ($some_not_important_condition) {
$logger->info('foo');
}
if ($some_important_condition) {
$logger->error('bar');
}
Thus, your app is always creating info
level log entries, and it's up to the run-time configuration to decide which to ingore and/or where they go. For example, you might print to the screen in development or log to Nagios in production.
来源:https://stackoverflow.com/questions/43665088/set-minimum-php-error-reporting-in-monolog-errorhandler