2013-07-05 17:28:10 +00:00
|
|
|
<?php
|
2022-05-24 06:39:20 +00:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2013-07-05 17:28:10 +00:00
|
|
|
/**
|
2016-07-21 15:07:57 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-03-31 08:49:10 +00:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2021-06-04 19:52:51 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2020-12-16 13:54:15 +00:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 18:57:53 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 10:44:34 +00:00
|
|
|
*
|
2013-07-05 17:28:10 +00:00
|
|
|
*/
|
|
|
|
namespace OC\Log;
|
|
|
|
|
2022-05-24 06:39:20 +00:00
|
|
|
use Error;
|
2015-04-30 09:52:30 +00:00
|
|
|
use OCP\ILogger;
|
2022-05-24 06:39:20 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Throwable;
|
2013-07-05 17:28:10 +00:00
|
|
|
|
|
|
|
class ErrorHandler {
|
2023-06-28 05:53:43 +00:00
|
|
|
public function __construct(
|
|
|
|
private LoggerInterface $logger,
|
|
|
|
) {
|
2022-05-24 06:39:20 +00:00
|
|
|
}
|
2013-07-05 17:28:10 +00:00
|
|
|
|
2014-01-31 11:28:21 +00:00
|
|
|
/**
|
2022-05-24 06:39:20 +00:00
|
|
|
* Remove password in URLs
|
2014-01-31 11:28:21 +00:00
|
|
|
*/
|
2022-05-24 06:39:20 +00:00
|
|
|
private static function removePassword(string $msg): string {
|
2021-03-27 17:41:43 +00:00
|
|
|
return preg_replace('#//(.*):(.*)@#', '//xxx:xxx@', $msg);
|
2014-01-31 11:28:21 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 06:39:20 +00:00
|
|
|
/**
|
|
|
|
* Fatal errors handler
|
|
|
|
*/
|
|
|
|
public function onShutdown(): void {
|
2013-07-05 17:28:10 +00:00
|
|
|
$error = error_get_last();
|
2022-05-24 06:39:20 +00:00
|
|
|
if ($error) {
|
2013-07-05 17:28:10 +00:00
|
|
|
$msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line'];
|
2022-05-24 06:39:20 +00:00
|
|
|
$this->logger->critical(self::removePassword($msg), ['app' => 'PHP']);
|
2013-07-05 17:28:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-19 10:35:22 +00:00
|
|
|
/**
|
2022-05-24 06:39:20 +00:00
|
|
|
* Uncaught exception handler
|
2015-05-19 10:35:22 +00:00
|
|
|
*/
|
2022-05-24 06:39:20 +00:00
|
|
|
public function onException(Throwable $exception): void {
|
2015-05-19 10:35:22 +00:00
|
|
|
$class = get_class($exception);
|
|
|
|
$msg = $exception->getMessage();
|
|
|
|
$msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine();
|
2022-05-24 06:39:20 +00:00
|
|
|
$this->logger->critical(self::removePassword($msg), ['app' => 'PHP']);
|
2013-07-05 17:28:10 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 06:39:20 +00: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 12:24:21 +00:00
|
|
|
if (!(error_reporting() & $number)) {
|
2022-05-24 06:39:20 +00:00
|
|
|
return true;
|
2013-07-05 17:28:10 +00:00
|
|
|
}
|
|
|
|
$msg = $message . ' at ' . $file . '#' . $line;
|
2022-05-24 06:39:20 +00:00
|
|
|
$e = new Error(self::removePassword($msg));
|
|
|
|
$this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']);
|
|
|
|
return true;
|
2014-01-31 12:27:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 06:39:20 +00: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 12:27:51 +00:00
|
|
|
$msg = $message . ' at ' . $file . '#' . $line;
|
2022-05-24 06:39:20 +00:00
|
|
|
$e = new Error(self::removePassword($msg));
|
|
|
|
$this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']);
|
|
|
|
return true;
|
2021-04-26 12:18:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 06:39:20 +00:00
|
|
|
private static function errnoToLogLevel(int $errno): int {
|
2023-06-28 05:53:43 +00:00
|
|
|
return match ($errno) {
|
|
|
|
E_USER_WARNING => ILogger::WARN,
|
|
|
|
E_DEPRECATED, E_USER_DEPRECATED => ILogger::DEBUG,
|
|
|
|
E_USER_NOTICE => ILogger::INFO,
|
|
|
|
default => ILogger::ERROR,
|
|
|
|
};
|
2013-07-05 17:28:10 +00:00
|
|
|
}
|
|
|
|
}
|