编辑:我创建了一个package,它实现了下面描述的行为。
Yii2 的错误处理程序不能这样配置。但是可以创建自己的错误处理程序,扩展yii\web\ErrorHandler(或yii\console\ErrorHandler,如果需要)。
namespace app\web;
use yii\web\ErrorHandler as BaseErrorHandler;
class ErrorHandler extends BaseErrorHandler {
/**
* @var array Used to specify which errors this handler should process.
*
* Default is ['fatal' => true, 'catchable' => E_ALL | E_STRICT ]
*
* E_ALL | E_STRICT is a default from set_error_handler() documentation.
*
* Set
* 'catchable' => false
* to disable catchable error handling with this ErrorHandler.
*
* You can also explicitly specify, which error types to process, i. e.:
* 'catchable' => E_ALL & ~E_NOTICE & ~E_STRICT
*/
public $error_types;
/**
* @var boolean Used to specify display_errors php ini setting
*/
public $display_errors = false;
/**
* @var string Used to reserve memory for fatal error handler.
*/
private $_memoryReserve;
/**
* @var \Exception from HHVM error that stores backtrace
*/
private $_hhvmException;
/**
* Register this error handler
*/
public function register()
{
// by default process all errors
// E_ALL | E_STRICT is a default from set_error_handler() documentation
$default_error_types = [ 'fatal' => true, 'catchable' => E_ALL | E_STRICT ];
// merge with application configuration
$error_types = array_merge($default_error_types, (array) $this->error_types);
ini_set('display_errors', $this->display_errors);
set_exception_handler([$this, 'handleException']);
if (defined('HHVM_VERSION')) {
set_error_handler([$this, 'handleHhvmError'], $error_types['catchable']);
} else {
set_error_handler([$this, 'handleError'], $error_types['catchable']);
}
if ($this->memoryReserveSize > 0) {
$this->_memoryReserve = str_repeat('x', $this->memoryReserveSize);
}
if ($error_types['fatal']) {
register_shutdown_function([$this, 'handleFatalError']);
}
}
}
那么就可以配置错误处理器了:
'components' => [
'errorHandler' => [
'class' => 'app\web\ErrorHandler',
'error_types' => [
'fatal' => true,
'catchable' => YII_DEBUG ? (E_ALL | E_STRICT) : false
],
'display_errors' => ini_get('display_errors')
],
],
在这个示例配置中,我们说错误处理程序应该始终处理致命错误,但只有在我们处于调试模式时才处理可捕获的错误。在生产模式下,所有可捕获的错误都将由内部 php 错误处理程序处理,如果您对其进行配置,则会出现在错误日志中。
display_errors 据说从php.ini 或.htaccess 继承服务器php 配置。