2013-07-05 13:28:10 -04:00
|
|
|
<?php
|
2022-05-24 02:39:20 -04:00
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2013-07-05 13:28:10 -04:00
|
|
|
/**
|
2024-05-23 03:26:56 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2013-07-05 13:28:10 -04:00
|
|
|
*/
|
|
|
|
|
namespace OC\Log;
|
|
|
|
|
|
2022-05-24 02:39:20 -04:00
|
|
|
use Error;
|
2015-04-30 05:52:30 -04:00
|
|
|
use OCP\ILogger;
|
2022-05-24 02:39:20 -04:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
|
use Throwable;
|
2013-07-05 13:28:10 -04:00
|
|
|
|
|
|
|
|
class ErrorHandler {
|
2023-06-28 01:53:43 -04:00
|
|
|
public function __construct(
|
|
|
|
|
private LoggerInterface $logger,
|
|
|
|
|
) {
|
2022-05-24 02:39:20 -04:00
|
|
|
}
|
2013-07-05 13:28:10 -04:00
|
|
|
|
2014-01-31 06:28:21 -05:00
|
|
|
/**
|
2022-05-24 02:39:20 -04:00
|
|
|
* Remove password in URLs
|
2014-01-31 06:28:21 -05:00
|
|
|
*/
|
2022-05-24 02:39:20 -04:00
|
|
|
private static function removePassword(string $msg): string {
|
2021-03-27 13:41:43 -04:00
|
|
|
return preg_replace('#//(.*):(.*)@#', '//xxx:xxx@', $msg);
|
2014-01-31 06:28:21 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 02:39:20 -04:00
|
|
|
/**
|
|
|
|
|
* Fatal errors handler
|
|
|
|
|
*/
|
|
|
|
|
public function onShutdown(): void {
|
2013-07-05 13:28:10 -04:00
|
|
|
$error = error_get_last();
|
2022-05-24 02:39:20 -04:00
|
|
|
if ($error) {
|
2013-07-05 13:28:10 -04:00
|
|
|
$msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line'];
|
2022-05-24 02:39:20 -04:00
|
|
|
$this->logger->critical(self::removePassword($msg), ['app' => 'PHP']);
|
2013-07-05 13:28:10 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 06:35:22 -04:00
|
|
|
/**
|
2022-05-24 02:39:20 -04:00
|
|
|
* Uncaught exception handler
|
2015-05-19 06:35:22 -04:00
|
|
|
*/
|
2022-05-24 02:39:20 -04:00
|
|
|
public function onException(Throwable $exception): void {
|
2015-05-19 06:35:22 -04:00
|
|
|
$class = get_class($exception);
|
|
|
|
|
$msg = $exception->getMessage();
|
|
|
|
|
$msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine();
|
2022-05-24 02:39:20 -04:00
|
|
|
$this->logger->critical(self::removePassword($msg), ['app' => 'PHP']);
|
2013-07-05 13:28:10 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 02:39:20 -04:00
|
|
|
/**
|
|
|
|
|
* Recoverable errors handler
|
|
|
|
|
*/
|
|
|
|
|
public function onError(int $number, string $message, string $file, int $line): bool {
|
Correctly skip suppressed errors in PHP 8.0
Applies the suggested transformation mentioned in
https://www.php.net/manual/en/migration80.incompatible.php,
> The @ operator will no longer silence fatal errors (E_ERROR,
> E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR,
> E_PARSE). Error handlers that expect error_reporting to be 0 when
> @ is used, should be adjusted to use a mask check instead
The new code still works on PHP 7, as error_reporting() already
returns 0 when diagnostics are suppressed.
This fixes https://github.com/nextcloud/server/issues/25807 in PHP 8.0.
For PHP 7.x, https://github.com/nextcloud/server/pull/22243 suppresses
the E_NOTICE message from the second session_start() call with the error
suppression operator @, and thus those E_NOTICE messages are still
logged in PHP 8.0.
See also https://github.com/nextcloud/server/issues/25806
Signed-off-by: Chih-Hsuan Yen <yan12125@gmail.com>
2021-06-23 08:24:21 -04:00
|
|
|
if (!(error_reporting() & $number)) {
|
2022-05-24 02:39:20 -04:00
|
|
|
return true;
|
2013-07-05 13:28:10 -04:00
|
|
|
}
|
|
|
|
|
$msg = $message . ' at ' . $file . '#' . $line;
|
2022-05-24 02:39:20 -04:00
|
|
|
$e = new Error(self::removePassword($msg));
|
|
|
|
|
$this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']);
|
|
|
|
|
return true;
|
2014-01-31 07:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 02:39:20 -04:00
|
|
|
/**
|
|
|
|
|
* Recoverable handler which catch all errors, warnings and notices
|
|
|
|
|
*/
|
|
|
|
|
public function onAll(int $number, string $message, string $file, int $line): bool {
|
2014-01-31 07:27:51 -05:00
|
|
|
$msg = $message . ' at ' . $file . '#' . $line;
|
2022-05-24 02:39:20 -04:00
|
|
|
$e = new Error(self::removePassword($msg));
|
|
|
|
|
$this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']);
|
|
|
|
|
return true;
|
2021-04-26 08:18:18 -04:00
|
|
|
}
|
|
|
|
|
|
2022-05-24 02:39:20 -04:00
|
|
|
private static function errnoToLogLevel(int $errno): int {
|
2023-06-28 01:53:43 -04:00
|
|
|
return match ($errno) {
|
2025-05-27 02:01:19 -04:00
|
|
|
E_WARNING, E_USER_WARNING => ILogger::WARN,
|
2023-06-28 01:53:43 -04:00
|
|
|
E_DEPRECATED, E_USER_DEPRECATED => ILogger::DEBUG,
|
2025-05-27 02:01:19 -04:00
|
|
|
E_NOTICE, E_USER_NOTICE => ILogger::INFO,
|
2023-06-28 01:53:43 -04:00
|
|
|
default => ILogger::ERROR,
|
|
|
|
|
};
|
2013-07-05 13:28:10 -04:00
|
|
|
}
|
|
|
|
|
}
|