ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
12namespace Monolog;
13
16
27{
28 private $logger;
29
32
35
36 private $fatalLevel;
38 private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);
39
40 public function __construct(LoggerInterface $logger)
41 {
42 $this->logger = $logger;
43 }
44
56 public static function register(LoggerInterface $logger, $errorLevelMap = array(), $exceptionLevel = null, $fatalLevel = null)
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 }
71
72 public function registerExceptionHandler($level = null, $callPrevious = true)
73 {
74 $prev = set_exception_handler(array($this, 'handleException'));
75 $this->uncaughtExceptionLevel = $level;
76 if ($callPrevious && $prev) {
77 $this->previousExceptionHandler = $prev;
78 }
79 }
80
81 public function registerErrorHandler(array $levelMap = array(), $callPrevious = true, $errorTypes = -1)
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 }
89
90 public function registerFatalHandler($level = null, $reservedMemorySize = 20)
91 {
92 register_shutdown_function(array($this, 'handleFatalError'));
93
94 $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
95 $this->fatalLevel = $level;
96 }
97
98 protected function defaultErrorLevelMap()
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 }
118
122 public function handleException($e)
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 }
136
140 public function handleError($code, $message, $file = '', $line = 0, $context = array())
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 }
155
159 public function handleFatalError()
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 }
172
173 private static function codeToString($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 }
210}
print $file
Monolog error handler.
registerErrorHandler(array $levelMap=array(), $callPrevious=true, $errorTypes=-1)
__construct(LoggerInterface $logger)
handleError($code, $message, $file='', $line=0, $context=array())
registerFatalHandler($level=null, $reservedMemorySize=20)
static codeToString($code)
registerExceptionHandler($level=null, $callPrevious=true)
Describes log levels.
Definition: LogLevel.php:9
$code
Definition: example_050.php:99
Describes a logger instance.
exit
Definition: login.php:54