41 private static $fatalErrors =
array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);
45 $this->logger = $logger;
59 public static function register(
LoggerInterface $logger, $errorLevelMap =
array(), $exceptionLevel = null, $fatalLevel = null)
61 $handler =
new static($logger);
62 if ($errorLevelMap !==
false) {
63 $handler->registerErrorHandler($errorLevelMap);
65 if ($exceptionLevel !==
false) {
66 $handler->registerExceptionHandler($exceptionLevel);
68 if ($fatalLevel !==
false) {
69 $handler->registerFatalHandler($fatalLevel);
77 $prev = set_exception_handler(
array($this,
'handleException'));
78 $this->uncaughtExceptionLevel = $level;
79 if ($callPrevious && $prev) {
80 $this->previousExceptionHandler = $prev;
86 $prev = set_error_handler(
array($this,
'handleError'), $errorTypes);
87 $this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap);
89 $this->previousErrorHandler = $prev ?:
true;
92 $this->handleOnlyReportedErrors = $handleOnlyReportedErrors;
97 register_shutdown_function(
array($this,
'handleFatalError'));
99 $this->reservedMemory = str_repeat(
' ', 1024 * $reservedMemorySize);
100 $this->fatalLevel = $level;
101 $this->hasFatalErrorHandler =
true;
107 E_ERROR => LogLevel::CRITICAL,
108 E_WARNING => LogLevel::WARNING,
109 E_PARSE => LogLevel::ALERT,
110 E_NOTICE => LogLevel::NOTICE,
111 E_CORE_ERROR => LogLevel::CRITICAL,
112 E_CORE_WARNING => LogLevel::WARNING,
113 E_COMPILE_ERROR => LogLevel::ALERT,
114 E_COMPILE_WARNING => LogLevel::WARNING,
115 E_USER_ERROR => LogLevel::ERROR,
116 E_USER_WARNING => LogLevel::WARNING,
117 E_USER_NOTICE => LogLevel::NOTICE,
118 E_STRICT => LogLevel::NOTICE,
119 E_RECOVERABLE_ERROR => LogLevel::ERROR,
120 E_DEPRECATED => LogLevel::NOTICE,
121 E_USER_DEPRECATED => LogLevel::NOTICE,
131 $this->uncaughtExceptionLevel === null ? LogLevel::ERROR : $this->uncaughtExceptionLevel,
132 sprintf(
'Uncaught Exception %s: "%s" at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()),
133 array(
'exception' => $e)
136 if ($this->previousExceptionHandler) {
137 call_user_func($this->previousExceptionHandler, $e);
148 if ($this->handleOnlyReportedErrors && !(error_reporting() &
$code)) {
153 if (!$this->hasFatalErrorHandler || !in_array(
$code, self::$fatalErrors,
true)) {
154 $level = isset($this->errorLevelMap[
$code]) ? $this->errorLevelMap[
$code] : LogLevel::CRITICAL;
155 $this->logger->log($level, self::codeToString($code).
': '.$message,
array(
'code' => $code,
'message' => $message,
'file' =>
$file,
'line' => $line));
158 if ($this->previousErrorHandler ===
true) {
160 } elseif ($this->previousErrorHandler) {
161 return call_user_func($this->previousErrorHandler,
$code, $message,
$file, $line, $context);
170 $this->reservedMemory = null;
172 $lastError = error_get_last();
173 if ($lastError && in_array($lastError[
'type'], self::$fatalErrors,
true)) {
175 $this->fatalLevel === null ? LogLevel::ALERT : $this->fatalLevel,
176 'Fatal Error ('.self::codeToString($lastError[
'type']).
'): '.$lastError[
'message'],
177 array(
'code' => $lastError[
'type'],
'message' => $lastError[
'message'],
'file' => $lastError[
'file'],
'line' => $lastError[
'line'])
180 if ($this->logger instanceof
Logger) {
181 foreach ($this->logger->getHandlers() as $handler) {
202 return 'E_CORE_ERROR';
204 return 'E_CORE_WARNING';
205 case E_COMPILE_ERROR:
206 return 'E_COMPILE_ERROR';
207 case E_COMPILE_WARNING:
208 return 'E_COMPILE_WARNING';
210 return 'E_USER_ERROR';
212 return 'E_USER_WARNING';
214 return 'E_USER_NOTICE';
217 case E_RECOVERABLE_ERROR:
218 return 'E_RECOVERABLE_ERROR';
220 return 'E_DEPRECATED';
221 case E_USER_DEPRECATED:
222 return 'E_USER_DEPRECATED';
225 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)
$handleOnlyReportedErrors
Create styles array
The data for the language used.
Describes a logger instance.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file