ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
ErrorHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog;
13 
17 
28 {
29  private $logger;
30 
33 
35  private $errorLevelMap;
37 
39  private $fatalLevel;
40  private $reservedMemory;
41  private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);
42 
43  public function __construct(LoggerInterface $logger)
44  {
45  $this->logger = $logger;
46  }
47 
59  public static function register(LoggerInterface $logger, $errorLevelMap = array(), $exceptionLevel = null, $fatalLevel = null)
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  }
74 
75  public function registerExceptionHandler($level = null, $callPrevious = true)
76  {
77  $prev = set_exception_handler(array($this, 'handleException'));
78  $this->uncaughtExceptionLevel = $level;
79  if ($callPrevious && $prev) {
80  $this->previousExceptionHandler = $prev;
81  }
82  }
83 
84  public function registerErrorHandler(array $levelMap = array(), $callPrevious = true, $errorTypes = -1, $handleOnlyReportedErrors = true)
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  }
94 
95  public function registerFatalHandler($level = null, $reservedMemorySize = 20)
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  }
103 
104  protected function defaultErrorLevelMap()
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  }
124 
128  public function handleException($e)
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  }
142 
146  public function handleError($code, $message, $file = '', $line = 0, $context = 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  }
164 
168  public function handleFatalError()
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  }
189 
190  private static function codeToString($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  }
227 }
Base Handler class providing the Handler structure.
Monolog error handler.
handleError($code, $message, $file='', $line=0, $context=array())
$code
Definition: example_050.php:99
registerErrorHandler(array $levelMap=array(), $callPrevious=true, $errorTypes=-1, $handleOnlyReportedErrors=true)
Monolog log channel.
Definition: Logger.php:27
registerFatalHandler($level=null, $reservedMemorySize=20)
registerExceptionHandler($level=null, $callPrevious=true)
__construct(LoggerInterface $logger)
static codeToString($code)
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