ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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
 

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 27 of file ErrorHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\ErrorHandler::__construct ( LoggerInterface  $logger)

Definition at line 43 of file ErrorHandler.php.

44  {
45  $this->logger = $logger;
46  }

Member Function Documentation

◆ codeToString()

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

Definition at line 190 of file ErrorHandler.php.

References $code.

191  {
192  switch ($code) {
193  case E_ERROR:
194  return 'E_ERROR';
195  case E_WARNING:
196  return 'E_WARNING';
197  case E_PARSE:
198  return 'E_PARSE';
199  case E_NOTICE:
200  return 'E_NOTICE';
201  case E_CORE_ERROR:
202  return 'E_CORE_ERROR';
203  case E_CORE_WARNING:
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';
209  case E_USER_ERROR:
210  return 'E_USER_ERROR';
211  case E_USER_WARNING:
212  return 'E_USER_WARNING';
213  case E_USER_NOTICE:
214  return 'E_USER_NOTICE';
215  case E_STRICT:
216  return 'E_STRICT';
217  case E_RECOVERABLE_ERROR:
218  return 'E_RECOVERABLE_ERROR';
219  case E_DEPRECATED:
220  return 'E_DEPRECATED';
221  case E_USER_DEPRECATED:
222  return 'E_USER_DEPRECATED';
223  }
224 
225  return 'Unknown PHP error';
226  }
$code
Definition: example_050.php:99

◆ defaultErrorLevelMap()

Monolog\ErrorHandler::defaultErrorLevelMap ( )
protected

Definition at line 104 of file ErrorHandler.php.

References array.

105  {
106  return array(
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,
122  );
123  }
Create styles array
The data for the language used.

◆ handleError()

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

Definition at line 146 of file ErrorHandler.php.

References $code, $file, $message, and array.

147  {
148  if ($this->handleOnlyReportedErrors && !(error_reporting() & $code)) {
149  return;
150  }
151 
152  // fatal error codes are ignored if a fatal error handler is present as well to avoid duplicate log entries
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));
156  }
157 
158  if ($this->previousErrorHandler === true) {
159  return false;
160  } elseif ($this->previousErrorHandler) {
161  return call_user_func($this->previousErrorHandler, $code, $message, $file, $line, $context);
162  }
163  }
$code
Definition: example_050.php:99
catch(Exception $e) $message
Create styles array
The data for the language used.
if(!file_exists("$old.txt")) if($old===$new) if(file_exists("$new.txt")) $file

◆ handleException()

Monolog\ErrorHandler::handleException (   $e)

Definition at line 128 of file ErrorHandler.php.

References array, and exit.

129  {
130  $this->logger->log(
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)
134  );
135 
136  if ($this->previousExceptionHandler) {
137  call_user_func($this->previousExceptionHandler, $e);
138  }
139 
140  exit(255);
141  }
Create styles array
The data for the language used.

◆ handleFatalError()

Monolog\ErrorHandler::handleFatalError ( )

Definition at line 168 of file ErrorHandler.php.

References $handler, and array.

169  {
170  $this->reservedMemory = null;
171 
172  $lastError = error_get_last();
173  if ($lastError && in_array($lastError['type'], self::$fatalErrors, true)) {
174  $this->logger->log(
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'])
178  );
179 
180  if ($this->logger instanceof Logger) {
181  foreach ($this->logger->getHandlers() as $handler) {
182  if ($handler instanceof AbstractHandler) {
183  $handler->close();
184  }
185  }
186  }
187  }
188  }
Create styles array
The data for the language used.
$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 59 of file ErrorHandler.php.

References $handler.

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

60  {
61  $handler = new static($logger);
62  if ($errorLevelMap !== false) {
63  $handler->registerErrorHandler($errorLevelMap);
64  }
65  if ($exceptionLevel !== false) {
66  $handler->registerExceptionHandler($exceptionLevel);
67  }
68  if ($fatalLevel !== false) {
69  $handler->registerFatalHandler($fatalLevel);
70  }
71 
72  return $handler;
73  }
$handler
+ Here is the caller graph for this function:

◆ registerErrorHandler()

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

Definition at line 84 of file ErrorHandler.php.

References array.

85  {
86  $prev = set_error_handler(array($this, 'handleError'), $errorTypes);
87  $this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap);
88  if ($callPrevious) {
89  $this->previousErrorHandler = $prev ?: true;
90  }
91 
92  $this->handleOnlyReportedErrors = $handleOnlyReportedErrors;
93  }
Create styles array
The data for the language used.

◆ registerExceptionHandler()

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

Definition at line 75 of file ErrorHandler.php.

References array.

76  {
77  $prev = set_exception_handler(array($this, 'handleException'));
78  $this->uncaughtExceptionLevel = $level;
79  if ($callPrevious && $prev) {
80  $this->previousExceptionHandler = $prev;
81  }
82  }
Create styles array
The data for the language used.

◆ registerFatalHandler()

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

Definition at line 95 of file ErrorHandler.php.

References array.

96  {
97  register_shutdown_function(array($this, 'handleFatalError'));
98 
99  $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
100  $this->fatalLevel = $level;
101  $this->hasFatalErrorHandler = true;
102  }
Create styles array
The data for the language used.

Field Documentation

◆ $errorLevelMap

Monolog\ErrorHandler::$errorLevelMap
private

Definition at line 35 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 41 of file ErrorHandler.php.

◆ $fatalLevel

Monolog\ErrorHandler::$fatalLevel
private

Definition at line 39 of file ErrorHandler.php.

◆ $handleOnlyReportedErrors

Monolog\ErrorHandler::$handleOnlyReportedErrors
private

Definition at line 36 of file ErrorHandler.php.

◆ $hasFatalErrorHandler

Monolog\ErrorHandler::$hasFatalErrorHandler
private

Definition at line 38 of file ErrorHandler.php.

◆ $logger

Monolog\ErrorHandler::$logger
private

Definition at line 29 of file ErrorHandler.php.

◆ $previousErrorHandler

Monolog\ErrorHandler::$previousErrorHandler
private

Definition at line 34 of file ErrorHandler.php.

◆ $previousExceptionHandler

Monolog\ErrorHandler::$previousExceptionHandler
private

Definition at line 31 of file ErrorHandler.php.

◆ $reservedMemory

Monolog\ErrorHandler::$reservedMemory
private

Definition at line 40 of file ErrorHandler.php.

◆ $uncaughtExceptionLevel

Monolog\ErrorHandler::$uncaughtExceptionLevel
private

Definition at line 32 of file ErrorHandler.php.


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