|
22 | 22 | use Config\Exceptions as ExceptionsConfig; |
23 | 23 | use Config\Paths; |
24 | 24 | use ErrorException; |
| 25 | +use Psr\Log\LogLevel; |
25 | 26 | use Throwable; |
26 | 27 |
|
27 | 28 | /** |
@@ -82,6 +83,10 @@ public function __construct(ExceptionsConfig $config, $request, ResponseInterfac |
82 | 83 | if (! isset($this->config->sensitiveDataInTrace)) { |
83 | 84 | $this->config->sensitiveDataInTrace = []; |
84 | 85 | } |
| 86 | + if (! isset($this->config->logDeprecationsOnly, $this->config->deprecationLogLevel)) { |
| 87 | + $this->config->logDeprecationsOnly = false; |
| 88 | + $this->config->deprecationLogLevel = LogLevel::WARNING; |
| 89 | + } |
85 | 90 | } |
86 | 91 |
|
87 | 92 | /** |
@@ -155,6 +160,10 @@ public function exceptionHandler(Throwable $exception) |
155 | 160 | */ |
156 | 161 | public function errorHandler(int $severity, string $message, ?string $file = null, ?int $line = null) |
157 | 162 | { |
| 163 | + if ($this->isDeprecationError($severity) && $this->config->logDeprecationsOnly) { |
| 164 | + return $this->handleDeprecationError($message, $file, $line); |
| 165 | + } |
| 166 | + |
158 | 167 | if (! (error_reporting() & $severity)) { |
159 | 168 | return; |
160 | 169 | } |
@@ -328,6 +337,37 @@ protected function determineCodes(Throwable $exception): array |
328 | 337 | return [$statusCode, $exitStatus]; |
329 | 338 | } |
330 | 339 |
|
| 340 | + private function isDeprecationError(int $error): bool |
| 341 | + { |
| 342 | + $deprecations = E_DEPRECATED | E_USER_DEPRECATED; |
| 343 | + |
| 344 | + return ($error & $deprecations) !== 0; |
| 345 | + } |
| 346 | + |
| 347 | + /** |
| 348 | + * @noRector \Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector |
| 349 | + * |
| 350 | + * @return true |
| 351 | + */ |
| 352 | + private function handleDeprecationError(string $message, ?string $file = null, ?int $line = null): bool |
| 353 | + { |
| 354 | + // Remove the trace of the error handler. |
| 355 | + $trace = array_slice(debug_backtrace(), 2); |
| 356 | + |
| 357 | + log_message( |
| 358 | + $this->config->deprecationLogLevel, |
| 359 | + "[DEPRECATED] {message} in {errFile} on line {errLine}.\n{trace}", |
| 360 | + [ |
| 361 | + 'message' => $message, |
| 362 | + 'errFile' => clean_path($file ?? ''), |
| 363 | + 'errLine' => $line ?? 0, |
| 364 | + 'trace' => self::renderBacktrace($trace), |
| 365 | + ] |
| 366 | + ); |
| 367 | + |
| 368 | + return true; |
| 369 | + } |
| 370 | + |
331 | 371 | // -------------------------------------------------------------------- |
332 | 372 | // Display Methods |
333 | 373 | // -------------------------------------------------------------------- |
|
0 commit comments