PluginProbe
ManageWP Worker / 4.9.25
ManageWP Worker v4.9.25
4.9.38 4.9.37 4.9.36 4.9.35 4.9.34 3.8.7 3.8.8 3.9.0 3.9.1 3.9.10 3.9.11 3.9.12 3.9.13 3.9.14 3.9.15 3.9.16 3.9.17 3.9.18 3.9.19 3.9.2 3.9.20 3.9.21 3.9.22 3.9.23 3.9.24 All 73 releases
worker / src / Monolog / ErrorHandler.php

ErrorHandler.php in ManageWP Worker 4.9.25, at src/Monolog/ErrorHandler.php

245 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
13 * Monolog error handler
14 *
15 * A facility to enable logging of runtime errors, exceptions and fatal errors.
16 *
17 * Quick setup: <code>ErrorHandler::register($logger);</code>
18 *
19 * @author Jordi Boggiano <j.boggiano@seld.be>
20 */
21 class Monolog_ErrorHandler
22 {
23 private $logger;
24
25 private $previousExceptionHandler;
26 private $uncaughtExceptionLevel;
27
28 private $previousErrorHandler;
29 private $errorLevelMap;
30
31 private $fatalLevel;
32 private $reservedMemory;
33 private static $fatalErrors = array(E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR);
34
35 public function __construct(Monolog_Psr_LoggerInterface $logger)
36 {
37 $this->logger = $logger;
38 }
39
40 /**
41 * Registers a new ErrorHandler for a given Logger
42 *
43 * By default it will handle errors, exceptions and fatal errors
44 *
45 * @param Monolog_Psr_LoggerInterface $logger
46 * @param array|false $errorLevelMap an array of E_* constant to LogLevel::* constant mapping, or false to disable error handling
47 * @param int|false $exceptionLevel a LogLevel::* constant, or false to disable exception handling
48 * @param int|false $fatalLevel a LogLevel::* constant, or false to disable fatal error handling
49 *
50 * @return Monolog_ErrorHandler
51 */
52 public static function register(Monolog_Psr_LoggerInterface $logger, $errorLevelMap = array(), $exceptionLevel = null, $fatalLevel = null)
53 {
54 $handler = new self($logger);
55 if ($errorLevelMap !== false) {
56 $handler->registerErrorHandler($errorLevelMap);
57 }
58 if ($exceptionLevel !== false) {
59 $handler->registerExceptionHandler($exceptionLevel);
60 }
61 if ($fatalLevel !== false) {
62 $handler->registerFatalHandler($fatalLevel);
63 }
64
65 return $handler;
66 }
67
68 public function registerExceptionHandler($level = null, $callPrevious = true)
69 {
70 $prev = set_exception_handler(array($this, 'handleException'));
71 $this->uncaughtExceptionLevel = $level;
72 if ($callPrevious && $prev) {
73 $this->previousExceptionHandler = $prev;
74 }
75 }
76
77 public function registerErrorHandler(array $levelMap = array(), $callPrevious = true, $errorTypes = -1)
78 {
79 $prev = set_error_handler(array($this, 'handleError'), $errorTypes);
80 $this->errorLevelMap = $this->arrayReplace($this->defaultErrorLevelMap(), $levelMap);
81 if ($callPrevious) {
82 $this->previousErrorHandler = $prev ? $prev : true;
83 }
84 }
85
86 /**
87 * Backport of PHP >=5.3 function array_replace
88 *
89 * @link http://www.php.net/manual/en/function.array-replace.php#94458
90 */
91 private function arrayReplace(array $array, array $array1)
92 {
93 $args = func_get_args();
94 $count = func_num_args();
95
96 for ($i = 0; $i < $count; ++$i) {
97 if (is_array($args[$i])) {
98 foreach ($args[$i] as $key => $val) {
99 $array[$key] = $val;
100 }
101 } else {
102 trigger_error(
103 __FUNCTION__.'(): Argument #'.($i + 1).' is not an array',
104 E_USER_WARNING
105 );
106
107 return null;
108 }
109 }
110
111 return $array;
112 }
113
114 public function registerFatalHandler($level = null, $reservedMemorySize = 20)
115 {
116 register_shutdown_function(array($this, 'handleFatalError'));
117
118 $this->reservedMemory = str_repeat(' ', 1024 * $reservedMemorySize);
119 $this->fatalLevel = $level;
120 }
121
122 protected function defaultErrorLevelMap()
123 {
124 $map = array(
125 E_ERROR => Monolog_Psr_LogLevel::CRITICAL,
126 E_WARNING => Monolog_Psr_LogLevel::WARNING,
127 E_PARSE => Monolog_Psr_LogLevel::ALERT,
128 E_NOTICE => Monolog_Psr_LogLevel::NOTICE,
129 E_CORE_ERROR => Monolog_Psr_LogLevel::CRITICAL,
130 E_CORE_WARNING => Monolog_Psr_LogLevel::WARNING,
131 E_COMPILE_ERROR => Monolog_Psr_LogLevel::ALERT,
132 E_COMPILE_WARNING => Monolog_Psr_LogLevel::WARNING,
133 E_USER_ERROR => Monolog_Psr_LogLevel::ERROR,
134 E_USER_WARNING => Monolog_Psr_LogLevel::WARNING,
135 E_USER_NOTICE => Monolog_Psr_LogLevel::NOTICE,
136 E_STRICT => Monolog_Psr_LogLevel::NOTICE,
137 E_RECOVERABLE_ERROR => Monolog_Psr_LogLevel::ERROR,
138 );
139
140 if (version_compare(PHP_VERSION, '5.3', '>=')) {
141 $map[E_USER_DEPRECATED] = Monolog_Psr_LogLevel::NOTICE;
142 $map[E_DEPRECATED] = Monolog_Psr_LogLevel::NOTICE;
143 }
144
145 return $map;
146 }
147
148 /**
149 * @private
150 */
151 public function handleException($e)
152 {
153 $this->logger->log(
154 $this->uncaughtExceptionLevel === null ? Monolog_Psr_LogLevel::ERROR : $this->uncaughtExceptionLevel,
155 'Uncaught exception',
156 array('exception' => $e)
157 );
158
159 if ($this->previousExceptionHandler) {
160 call_user_func($this->previousExceptionHandler, $e);
161 }
162 }
163
164 /**
165 * @private
166 */
167 public function handleError($code, $message, $file = '', $line = 0, $context = array())
168 {
169 if (!(error_reporting() & $code)) {
170 return;
171 }
172 $level = isset($this->errorLevelMap[$code]) ? $this->errorLevelMap[$code] : Monolog_Psr_LogLevel::CRITICAL;
173 $this->logger->log($level, self::codeToString($code).': '.$message, array('code' => $code, 'message' => $message, 'file' => $file, 'line' => $line));
174 if ($this->previousErrorHandler === true) {
175 return false;
176 } elseif ($this->previousErrorHandler) {
177 return call_user_func($this->previousErrorHandler, $code, $message, $file, $line, $context);
178 }
179 }
180
181 /**
182 * @private
183 */
184 public function handleFatalError()
185 {
186 $this->reservedMemory = null;
187
188 $lastError = error_get_last();
189 if ($lastError && in_array($lastError['type'], self::$fatalErrors)) {
190 $this->logger->log(
191 $this->fatalLevel === null ? Monolog_Psr_LogLevel::ALERT : $this->fatalLevel,
192 'Fatal Error ('.self::codeToString($lastError['type']).'): '.$lastError['message'],
193 array('code' => $lastError['type'], 'message' => $lastError['message'], 'file' => $lastError['file'], 'line' => $lastError['line'])
194 );
195 }
196 }
197
198 private static function codeToString($code)
199 {
200 switch ($code) {
201 case E_ERROR:
202 return 'E_ERROR';
203 case E_WARNING:
204 return 'E_WARNING';
205 case E_PARSE:
206 return 'E_PARSE';
207 case E_NOTICE:
208 return 'E_NOTICE';
209 case E_CORE_ERROR:
210 return 'E_CORE_ERROR';
211 case E_CORE_WARNING:
212 return 'E_CORE_WARNING';
213 case E_COMPILE_ERROR:
214 return 'E_COMPILE_ERROR';
215 case E_COMPILE_WARNING:
216 return 'E_COMPILE_WARNING';
217 case E_USER_ERROR:
218 return 'E_USER_ERROR';
219 case E_USER_WARNING:
220 return 'E_USER_WARNING';
221 case E_USER_NOTICE:
222 return 'E_USER_NOTICE';
223 case E_STRICT:
224 return 'E_STRICT';
225 case E_RECOVERABLE_ERROR:
226 return 'E_RECOVERABLE_ERROR';
227 case E_DEPRECATED:
228 return 'E_DEPRECATED';
229 case E_USER_DEPRECATED:
230 return 'E_USER_DEPRECATED';
231 }
232
233 if (version_compare(PHP_VERSION, '5.3', '>=')) {
234 switch ($code) {
235 case E_DEPRECATED:
236 return 'E_DEPRECATED';
237 case E_USER_DEPRECATED:
238 return 'E_USER_DEPRECATED';
239 }
240 }
241
242 return 'Unknown PHP error';
243 }
244 }
245