Log trigger_error() of deprecated code

This commit is contained in:
Art4 2025-01-31 15:36:08 +00:00
parent 02cde27a12
commit af96a957cd

View file

@ -197,7 +197,7 @@ class ErrorHandler
E_STRICT => LogLevel::NOTICE,
E_RECOVERABLE_ERROR => LogLevel::ERROR,
E_DEPRECATED => LogLevel::NOTICE,
E_USER_DEPRECATED => LogLevel::NOTICE,
E_USER_DEPRECATED => LogLevel::WARNING,
];
}
@ -248,17 +248,29 @@ class ErrorHandler
*/
public function handleError(int $code, string $message, string $file = '', int $line = 0, ?array $context = []): bool
{
if ($this->handleOnlyReportedErrors && !(error_reporting() & $code)) {
if ($this->handleOnlyReportedErrors && !(error_reporting() & $code) && $code !== E_USER_DEPRECATED) {
return true;
}
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($trace); // Exclude handleError from trace
if ($code === E_USER_DEPRECATED && $trace[0]['function'] ?? '' === 'trigger_error') {
$calledPlace = $trace[1] ?? [];
$message .= sprintf(
' It was called in `%s`%s.',
$calledPlace['file'],
isset($calledPlace['line']) ? ' in line ' . $calledPlace['line'] : ''
);
}
// fatal error codes are ignored if a fatal error handler is present as well to avoid duplicate log entries
if (!$this->hasFatalErrorHandler || !in_array($code, self::$fatalErrors, true)) {
$level = $this->errorLevelMap[$code] ?? LogLevel::CRITICAL;
$this->logger->log($level, self::codeToString($code).': '.$message, ['code' => $code, 'message' => $message, 'file' => $file, 'line' => $line]);
} else {
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
array_shift($trace); // Exclude handleError from trace
$this->lastFatalTrace = $trace;
}