EDIT:我创建了一个package https://github.com/cronfy/yii2-web-errorhandler实现下面描述的行为。
Yii2 的错误处理程序不能以这种方式配置。但可以创建自己的错误处理程序,扩展yii\web\ErrorHandler
(or 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 配置php.ini
or .htaccess
.