43 private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);
47 $this->logger = $logger;
61 public static function register(
LoggerInterface $logger, $errorLevelMap = array(), $exceptionLevel = null, $fatalLevel = null)
64 class_exists(
'\\Psr\\Log\\LogLevel',
true);
67 if ($errorLevelMap !==
false) {
68 $handler->registerErrorHandler($errorLevelMap);
70 if ($exceptionLevel !==
false) {
71 $handler->registerExceptionHandler($exceptionLevel);
73 if ($fatalLevel !==
false) {
74 $handler->registerFatalHandler($fatalLevel);
82 $prev = set_exception_handler(array($this,
'handleException'));
83 $this->uncaughtExceptionLevel = $level;
84 if ($callPrevious && $prev) {
85 $this->previousExceptionHandler = $prev;
89 public function registerErrorHandler(array $levelMap = array(), $callPrevious =
true, $errorTypes = -1, $handleOnlyReportedErrors =
true)
91 $prev = set_error_handler(array($this,
'handleError'), $errorTypes);
92 $this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap);
94 $this->previousErrorHandler = $prev ?:
true;
97 $this->handleOnlyReportedErrors = $handleOnlyReportedErrors;
102 register_shutdown_function(array($this,
'handleFatalError'));
104 $this->reservedMemory = str_repeat(
' ', 1024 * $reservedMemorySize);
105 $this->fatalLevel = $level;
106 $this->hasFatalErrorHandler =
true;
112 E_ERROR => LogLevel::CRITICAL,
113 E_WARNING => LogLevel::WARNING,
114 E_PARSE => LogLevel::ALERT,
115 E_NOTICE => LogLevel::NOTICE,
116 E_CORE_ERROR => LogLevel::CRITICAL,
117 E_CORE_WARNING => LogLevel::WARNING,
118 E_COMPILE_ERROR => LogLevel::ALERT,
119 E_COMPILE_WARNING => LogLevel::WARNING,
120 E_USER_ERROR => LogLevel::ERROR,
121 E_USER_WARNING => LogLevel::WARNING,
122 E_USER_NOTICE => LogLevel::NOTICE,
123 E_STRICT => LogLevel::NOTICE,
124 E_RECOVERABLE_ERROR => LogLevel::ERROR,
125 E_DEPRECATED => LogLevel::NOTICE,
126 E_USER_DEPRECATED => LogLevel::NOTICE,
136 $this->uncaughtExceptionLevel === null ? LogLevel::ERROR : $this->uncaughtExceptionLevel,
137 sprintf(
'Uncaught Exception %s: "%s" at %s line %s', Utils::getClass($e), $e->getMessage(), $e->getFile(), $e->getLine()),
138 array(
'exception' => $e)
141 if ($this->previousExceptionHandler) {
142 call_user_func($this->previousExceptionHandler, $e);
153 if ($this->handleOnlyReportedErrors && !(error_reporting() &
$code)) {
158 if (!$this->hasFatalErrorHandler || !in_array(
$code, self::$fatalErrors,
true)) {
159 $level = isset($this->errorLevelMap[
$code]) ? $this->errorLevelMap[
$code] : LogLevel::CRITICAL;
160 $this->logger->log($level, self::codeToString($code).
': '.
$message, array(
'code' => $code,
'message' => $message,
'file' => $file,
'line' => $line));
165 $trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS);
167 $this->lastFatalTrace = $trace;
170 if ($this->previousErrorHandler ===
true) {
172 } elseif ($this->previousErrorHandler) {
182 $this->reservedMemory = null;
184 $lastError = error_get_last();
185 if ($lastError && in_array($lastError[
'type'], self::$fatalErrors,
true)) {
187 $this->fatalLevel === null ? LogLevel::ALERT : $this->fatalLevel,
188 'Fatal Error ('.self::codeToString($lastError[
'type']).
'): '.$lastError[
'message'],
189 array(
'code' => $lastError[
'type'],
'message' => $lastError[
'message'],
'file' => $lastError[
'file'],
'line' => $lastError[
'line'],
'trace' => $this->lastFatalTrace)
192 if ($this->logger instanceof
Logger) {
193 foreach ($this->logger->getHandlers() as
$handler) {
214 return 'E_CORE_ERROR';
216 return 'E_CORE_WARNING';
217 case E_COMPILE_ERROR:
218 return 'E_COMPILE_ERROR';
219 case E_COMPILE_WARNING:
220 return 'E_COMPILE_WARNING';
222 return 'E_USER_ERROR';
224 return 'E_USER_WARNING';
226 return 'E_USER_NOTICE';
229 case E_RECOVERABLE_ERROR:
230 return 'E_RECOVERABLE_ERROR';
232 return 'E_DEPRECATED';
233 case E_USER_DEPRECATED:
234 return 'E_USER_DEPRECATED';
237 return 'Unknown PHP error';
Base Handler class providing the Handler structure.
handleError($code, $message, $file='', $line=0, $context=array())
registerErrorHandler(array $levelMap=array(), $callPrevious=true, $errorTypes=-1, $handleOnlyReportedErrors=true)
registerFatalHandler($level=null, $reservedMemorySize=20)
registerExceptionHandler($level=null, $callPrevious=true)
__construct(LoggerInterface $logger)
$previousExceptionHandler
static codeToString($code)
catch(Exception $e) $message
$handleOnlyReportedErrors
Describes a logger instance.