ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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)
 
 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
 
 $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 26 of file ErrorHandler.php.

Constructor & Destructor Documentation

◆ __construct()

Monolog\ErrorHandler::__construct ( LoggerInterface  $logger)

Definition at line 40 of file ErrorHandler.php.

41  {
42  $this->logger = $logger;
43  }

Member Function Documentation

◆ codeToString()

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

Definition at line 173 of file ErrorHandler.php.

References $code.

174  {
175  switch ($code) {
176  case E_ERROR:
177  return 'E_ERROR';
178  case E_WARNING:
179  return 'E_WARNING';
180  case E_PARSE:
181  return 'E_PARSE';
182  case E_NOTICE:
183  return 'E_NOTICE';
184  case E_CORE_ERROR:
185  return 'E_CORE_ERROR';
186  case E_CORE_WARNING:
187  return 'E_CORE_WARNING';
188  case E_COMPILE_ERROR:
189  return 'E_COMPILE_ERROR';
190  case E_COMPILE_WARNING:
191  return 'E_COMPILE_WARNING';
192  case E_USER_ERROR:
193  return 'E_USER_ERROR';
194  case E_USER_WARNING:
195  return 'E_USER_WARNING';
196  case E_USER_NOTICE:
197  return 'E_USER_NOTICE';
198  case E_STRICT:
199  return 'E_STRICT';
200  case E_RECOVERABLE_ERROR:
201  return 'E_RECOVERABLE_ERROR';
202  case E_DEPRECATED:
203  return 'E_DEPRECATED';
204  case E_USER_DEPRECATED:
205  return 'E_USER_DEPRECATED';
206  }
207 
208  return 'Unknown PHP error';
209  }
$code
Definition: example_050.php:99

◆ defaultErrorLevelMap()

Monolog\ErrorHandler::defaultErrorLevelMap ( )
protected

Definition at line 98 of file ErrorHandler.php.

99  {
100  return array(
101  E_ERROR => LogLevel::CRITICAL,
102  E_WARNING => LogLevel::WARNING,
103  E_PARSE => LogLevel::ALERT,
104  E_NOTICE => LogLevel::NOTICE,
105  E_CORE_ERROR => LogLevel::CRITICAL,
106  E_CORE_WARNING => LogLevel::WARNING,
107  E_COMPILE_ERROR => LogLevel::ALERT,
108  E_COMPILE_WARNING => LogLevel::WARNING,
109  E_USER_ERROR => LogLevel::ERROR,
110  E_USER_WARNING => LogLevel::WARNING,
111  E_USER_NOTICE => LogLevel::NOTICE,
112  E_STRICT => LogLevel::NOTICE,
113  E_RECOVERABLE_ERROR => LogLevel::ERROR,
114  E_DEPRECATED => LogLevel::NOTICE,
115  E_USER_DEPRECATED => LogLevel::NOTICE,
116  );
117  }

◆ handleError()

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

Definition at line 140 of file ErrorHandler.php.

References $code, and $file.

141  {
142  if (!(error_reporting() & $code)) {
143  return;
144  }
145 
146  $level = isset($this->errorLevelMap[$code]) ? $this->errorLevelMap[$code] : LogLevel::CRITICAL;
147  $this->logger->log($level, self::codeToString($code).': '.$message, array('code' => $code, 'message' => $message, 'file' => $file, 'line' => $line));
148 
149  if ($this->previousErrorHandler === true) {
150  return false;
151  } elseif ($this->previousErrorHandler) {
152  return call_user_func($this->previousErrorHandler, $code, $message, $file, $line, $context);
153  }
154  }
print $file
$code
Definition: example_050.php:99

◆ handleException()

Monolog\ErrorHandler::handleException (   $e)

Definition at line 122 of file ErrorHandler.php.

References exit.

123  {
124  $this->logger->log(
125  $this->uncaughtExceptionLevel === null ? LogLevel::ERROR : $this->uncaughtExceptionLevel,
126  sprintf('Uncaught Exception %s: "%s" at %s line %s', get_class($e), $e->getMessage(), $e->getFile(), $e->getLine()),
127  array('exception' => $e)
128  );
129 
130  if ($this->previousExceptionHandler) {
131  call_user_func($this->previousExceptionHandler, $e);
132  }
133 
134  exit(255);
135  }
exit
Definition: login.php:54

◆ handleFatalError()

Monolog\ErrorHandler::handleFatalError ( )

Definition at line 159 of file ErrorHandler.php.

160  {
161  $this->reservedMemory = null;
162 
163  $lastError = error_get_last();
164  if ($lastError && in_array($lastError['type'], self::$fatalErrors)) {
165  $this->logger->log(
166  $this->fatalLevel === null ? LogLevel::ALERT : $this->fatalLevel,
167  'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'],
168  array('code' => $lastError['type'], 'message' => $lastError['message'], 'file' => $lastError['file'], 'line' => $lastError['line'])
169  );
170  }
171  }

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

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

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

◆ registerErrorHandler()

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

Definition at line 81 of file ErrorHandler.php.

82  {
83  $prev = set_error_handler(array($this, 'handleError'), $errorTypes);
84  $this->errorLevelMap = array_replace($this->defaultErrorLevelMap(), $levelMap);
85  if ($callPrevious) {
86  $this->previousErrorHandler = $prev ?: true;
87  }
88  }

◆ registerExceptionHandler()

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

Definition at line 72 of file ErrorHandler.php.

73  {
74  $prev = set_exception_handler(array($this, 'handleException'));
75  $this->uncaughtExceptionLevel = $level;
76  if ($callPrevious && $prev) {
77  $this->previousExceptionHandler = $prev;
78  }
79  }

◆ registerFatalHandler()

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

Definition at line 90 of file ErrorHandler.php.

91  {
92  register_shutdown_function(array($this, 'handleFatalError'));
93 
94  $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
95  $this->fatalLevel = $level;
96  }

Field Documentation

◆ $errorLevelMap

Monolog\ErrorHandler::$errorLevelMap
private

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

◆ $fatalLevel

Monolog\ErrorHandler::$fatalLevel
private

Definition at line 36 of file ErrorHandler.php.

◆ $logger

Monolog\ErrorHandler::$logger
private

Definition at line 28 of file ErrorHandler.php.

◆ $previousErrorHandler

Monolog\ErrorHandler::$previousErrorHandler
private

Definition at line 33 of file ErrorHandler.php.

◆ $previousExceptionHandler

Monolog\ErrorHandler::$previousExceptionHandler
private

Definition at line 30 of file ErrorHandler.php.

◆ $reservedMemory

Monolog\ErrorHandler::$reservedMemory
private

Definition at line 37 of file ErrorHandler.php.

◆ $uncaughtExceptionLevel

Monolog\ErrorHandler::$uncaughtExceptionLevel
private

Definition at line 31 of file ErrorHandler.php.


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