ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Monolog\ErrorHandler Class Reference

Monolog error handler. More...

+ Collaboration diagram for Monolog\ErrorHandler:

Public Member Functions

 __construct (LoggerInterface $logger)
 
 registerExceptionHandler ($level=null, $callPrevious=true)
 
 registerErrorHandler (array $levelMap=array(), $callPrevious=true, $errorTypes=-1, $handleOnlyReportedErrors=true)
 
 registerFatalHandler ($level=null, $reservedMemorySize=20)
 
 handleException ($e)
 
 handleError ($code, $message, $file='', $line=0, $context=array())
 
 handleFatalError ()
 

Static Public Member Functions

static register (LoggerInterface $logger, $errorLevelMap=array(), $exceptionLevel=null, $fatalLevel=null)
 Registers a new ErrorHandler for a given Logger. More...
 

Protected Member Functions

 defaultErrorLevelMap ()
 

Static Private Member Functions

static codeToString ($code)
 

Private Attributes

 $logger
 
 $previousExceptionHandler
 
 $uncaughtExceptionLevel
 
 $previousErrorHandler
 
 $errorLevelMap
 
 $handleOnlyReportedErrors
 
 $hasFatalErrorHandler
 
 $fatalLevel
 
 $reservedMemory
 
 $lastFatalTrace
 

Static Private Attributes

static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR)
 

Detailed Description

Monolog error handler.

A facility to enable logging of runtime errors, exceptions and fatal errors.

Quick setup: ErrorHandler::register($logger);

Author
Jordi Boggiano j.bog.nosp@m.gian.nosp@m.o@sel.nosp@m.d.be

Definition at line 28 of file ErrorHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\ErrorHandler::__construct ( LoggerInterface  $logger)

Definition at line 45 of file ErrorHandler.php.

46 {
47 $this->logger = $logger;
48 }

Member Function Documentation

◆ codeToString()

static Monolog\ErrorHandler::codeToString (   $code)
staticprivate

Definition at line 202 of file ErrorHandler.php.

203 {
204 switch ($code) {
205 case E_ERROR:
206 return 'E_ERROR';
207 case E_WARNING:
208 return 'E_WARNING';
209 case E_PARSE:
210 return 'E_PARSE';
211 case E_NOTICE:
212 return 'E_NOTICE';
213 case E_CORE_ERROR:
214 return 'E_CORE_ERROR';
215 case E_CORE_WARNING:
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';
221 case E_USER_ERROR:
222 return 'E_USER_ERROR';
223 case E_USER_WARNING:
224 return 'E_USER_WARNING';
225 case E_USER_NOTICE:
226 return 'E_USER_NOTICE';
227 case E_STRICT:
228 return 'E_STRICT';
229 case E_RECOVERABLE_ERROR:
230 return 'E_RECOVERABLE_ERROR';
231 case E_DEPRECATED:
232 return 'E_DEPRECATED';
233 case E_USER_DEPRECATED:
234 return 'E_USER_DEPRECATED';
235 }
236
237 return 'Unknown PHP error';
238 }
$code
Definition: example_050.php:99

References $code.

◆ defaultErrorLevelMap()

Monolog\ErrorHandler::defaultErrorLevelMap ( )
protected

Definition at line 109 of file ErrorHandler.php.

110 {
111 return array(
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,
127 );
128 }

◆ handleError()

Monolog\ErrorHandler::handleError (   $code,
  $message,
  $file = '',
  $line = 0,
  $context = array() 
)

Definition at line 151 of file ErrorHandler.php.

152 {
153 if ($this->handleOnlyReportedErrors && !(error_reporting() & $code)) {
154 return;
155 }
156
157 // fatal error codes are ignored if a fatal error handler is present as well to avoid duplicate log entries
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));
161 } else {
162 // http://php.net/manual/en/function.debug-backtrace.php
163 // As of 5.3.6, DEBUG_BACKTRACE_IGNORE_ARGS option was added.
164 // Any version less than 5.3.6 must use the DEBUG_BACKTRACE_IGNORE_ARGS constant value '2'.
165 $trace = debug_backtrace((PHP_VERSION_ID < 50306) ? 2 : DEBUG_BACKTRACE_IGNORE_ARGS);
166 array_shift($trace); // Exclude handleError from trace
167 $this->lastFatalTrace = $trace;
168 }
169
170 if ($this->previousErrorHandler === true) {
171 return false;
172 } elseif ($this->previousErrorHandler) {
173 return call_user_func($this->previousErrorHandler, $code, $message, $file, $line, $context);
174 }
175 }
catch(Exception $e) $message
$context
Definition: webdav.php:25

References $code, $context, and $message.

◆ handleException()

Monolog\ErrorHandler::handleException (   $e)

Definition at line 133 of file ErrorHandler.php.

134 {
135 $this->logger->log(
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)
139 );
140
141 if ($this->previousExceptionHandler) {
142 call_user_func($this->previousExceptionHandler, $e);
143 }
144
145 exit(255);
146 }
exit
Definition: backend.php:16
static getClass($object)
Definition: Utils.php:19

References exit.

◆ handleFatalError()

Monolog\ErrorHandler::handleFatalError ( )

Definition at line 180 of file ErrorHandler.php.

181 {
182 $this->reservedMemory = null;
183
184 $lastError = error_get_last();
185 if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) {
186 $this->logger->log(
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)
190 );
191
192 if ($this->logger instanceof Logger) {
193 foreach ($this->logger->getHandlers() as $handler) {
194 if ($handler instanceof AbstractHandler) {
195 $handler->close();
196 }
197 }
198 }
199 }
200 }
$handler

References $handler.

◆ register()

static Monolog\ErrorHandler::register ( LoggerInterface  $logger,
  $errorLevelMap = array(),
  $exceptionLevel = null,
  $fatalLevel = null 
)
static

Registers a new ErrorHandler for a given Logger.

By default it will handle errors, exceptions and fatal errors

Parameters
LoggerInterface$logger
array | false$errorLevelMapan array of E_* constant to LogLevel::* constant mapping, or false to disable error handling
int | false$exceptionLevela LogLevel::* constant, or false to disable exception handling
int | false$fatalLevela LogLevel::* constant, or false to disable fatal error handling
Returns
ErrorHandler

Definition at line 61 of file ErrorHandler.php.

62 {
63 //Forces the autoloader to run for LogLevel. Fixes an autoload issue at compile-time on PHP5.3. See https://github.com/Seldaek/monolog/pull/929
64 class_exists('\\Psr\\Log\\LogLevel', true);
65
66 $handler = new static($logger);
67 if ($errorLevelMap !== false) {
68 $handler->registerErrorHandler($errorLevelMap);
69 }
70 if ($exceptionLevel !== false) {
71 $handler->registerExceptionHandler($exceptionLevel);
72 }
73 if ($fatalLevel !== false) {
74 $handler->registerFatalHandler($fatalLevel);
75 }
76
77 return $handler;
78 }

References $handler.

Referenced by Monolog\Handler\PHPConsoleHandlerTest\testError().

+ Here is the caller graph for this function:

◆ registerErrorHandler()

Monolog\ErrorHandler::registerErrorHandler ( array  $levelMap = array(),
  $callPrevious = true,
  $errorTypes = -1,
  $handleOnlyReportedErrors = true 
)

Definition at line 89 of file ErrorHandler.php.

90 {
91 $prev = set_error_handler(array($this, 'handleError'), $errorTypes);
92 $this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap);
93 if ($callPrevious) {
94 $this->previousErrorHandler = $prev ?: true;
95 }
96
97 $this->handleOnlyReportedErrors = $handleOnlyReportedErrors;
98 }

◆ registerExceptionHandler()

Monolog\ErrorHandler::registerExceptionHandler (   $level = null,
  $callPrevious = true 
)

Definition at line 80 of file ErrorHandler.php.

81 {
82 $prev = set_exception_handler(array($this, 'handleException'));
83 $this->uncaughtExceptionLevel = $level;
84 if ($callPrevious && $prev) {
85 $this->previousExceptionHandler = $prev;
86 }
87 }

◆ registerFatalHandler()

Monolog\ErrorHandler::registerFatalHandler (   $level = null,
  $reservedMemorySize = 20 
)

Definition at line 100 of file ErrorHandler.php.

101 {
102 register_shutdown_function(array($this, 'handleFatalError'));
103
104 $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
105 $this->fatalLevel = $level;
106 $this->hasFatalErrorHandler = true;
107 }

Field Documentation

◆ $errorLevelMap

Monolog\ErrorHandler::$errorLevelMap
private

Definition at line 36 of file ErrorHandler.php.

◆ $fatalErrors

Monolog\ErrorHandler::$fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR)
staticprivate

Definition at line 43 of file ErrorHandler.php.

◆ $fatalLevel

Monolog\ErrorHandler::$fatalLevel
private

Definition at line 40 of file ErrorHandler.php.

◆ $handleOnlyReportedErrors

Monolog\ErrorHandler::$handleOnlyReportedErrors
private

Definition at line 37 of file ErrorHandler.php.

◆ $hasFatalErrorHandler

Monolog\ErrorHandler::$hasFatalErrorHandler
private

Definition at line 39 of file ErrorHandler.php.

◆ $lastFatalTrace

Monolog\ErrorHandler::$lastFatalTrace
private

Definition at line 42 of file ErrorHandler.php.

◆ $logger

Monolog\ErrorHandler::$logger
private

Definition at line 30 of file ErrorHandler.php.

◆ $previousErrorHandler

Monolog\ErrorHandler::$previousErrorHandler
private

Definition at line 35 of file ErrorHandler.php.

◆ $previousExceptionHandler

Monolog\ErrorHandler::$previousExceptionHandler
private

Definition at line 32 of file ErrorHandler.php.

◆ $reservedMemory

Monolog\ErrorHandler::$reservedMemory
private

Definition at line 41 of file ErrorHandler.php.

◆ $uncaughtExceptionLevel

Monolog\ErrorHandler::$uncaughtExceptionLevel
private

Definition at line 33 of file ErrorHandler.php.


The documentation for this class was generated from the following file: