2013-07-05 13:28:10 -04:00
|
|
|
<?php
|
|
|
|
|
/**
|
2016-07-21 11:07:57 -04:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2015-03-26 06:44:34 -04:00
|
|
|
* @author Bart Visscher <bartv@thisnet.nl>
|
2016-05-26 13:56:05 -04:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-03-31 04:49:10 -04:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2020-12-16 08:54:15 -05:00
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2015-03-26 06:44:34 -04:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-06-25 05:43:55 -04:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-03-26 06:44:34 -04: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 13:57:53 -05:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-03-26 06:44:34 -04:00
|
|
|
*
|
2013-07-05 13:28:10 -04:00
|
|
|
*/
|
2015-02-26 05:37:37 -05:00
|
|
|
|
2013-07-05 13:28:10 -04:00
|
|
|
namespace OC\Log;
|
|
|
|
|
|
2015-04-30 05:52:30 -04:00
|
|
|
use OCP\ILogger;
|
2013-07-05 13:28:10 -04:00
|
|
|
|
|
|
|
|
class ErrorHandler {
|
2015-04-30 05:52:30 -04:00
|
|
|
/** @var ILogger */
|
2013-07-05 13:28:10 -04:00
|
|
|
private static $logger;
|
|
|
|
|
|
2014-01-31 06:28:21 -05:00
|
|
|
/**
|
2014-05-19 11:50:53 -04:00
|
|
|
* remove password in URLs
|
2014-01-31 06:28:21 -05:00
|
|
|
* @param string $msg
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
2014-02-04 05:11:24 -05:00
|
|
|
protected static function removePassword($msg) {
|
2014-01-31 06:28:21 -05:00
|
|
|
return preg_replace('/\/\/(.*):(.*)@/', '//xxx:xxx@', $msg);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-05 09:12:57 -04:00
|
|
|
public static function register($debug = false) {
|
2013-07-05 13:28:10 -04:00
|
|
|
$handler = new ErrorHandler();
|
|
|
|
|
|
2014-01-31 07:27:51 -05:00
|
|
|
if ($debug) {
|
2020-03-26 04:30:18 -04:00
|
|
|
set_error_handler([$handler, 'onAll'], E_ALL);
|
2016-04-18 16:30:01 -04:00
|
|
|
if (\OC::$CLI) {
|
2020-03-26 04:30:18 -04:00
|
|
|
set_exception_handler(['OC_Template', 'printExceptionErrorPage']);
|
2016-04-18 16:30:01 -04:00
|
|
|
}
|
2014-01-31 07:27:51 -05:00
|
|
|
} else {
|
2020-03-26 04:30:18 -04:00
|
|
|
set_error_handler([$handler, 'onError']);
|
2014-01-31 07:27:51 -05:00
|
|
|
}
|
2020-03-26 04:30:18 -04:00
|
|
|
register_shutdown_function([$handler, 'onShutdown']);
|
|
|
|
|
set_exception_handler([$handler, 'onException']);
|
2013-07-05 13:28:10 -04:00
|
|
|
}
|
|
|
|
|
|
2015-04-30 05:52:30 -04:00
|
|
|
public static function setLogger(ILogger $logger) {
|
2013-07-05 13:28:10 -04:00
|
|
|
self::$logger = $logger;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Fatal errors handler
|
|
|
|
|
public static function onShutdown() {
|
|
|
|
|
$error = error_get_last();
|
2020-04-10 08:19:56 -04:00
|
|
|
if ($error && self::$logger) {
|
2013-07-05 13:28:10 -04:00
|
|
|
//ob_end_clean();
|
|
|
|
|
$msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line'];
|
2020-03-26 04:30:18 -04:00
|
|
|
self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']);
|
2013-07-05 13:28:10 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-19 06:35:22 -04:00
|
|
|
/**
|
|
|
|
|
* Uncaught exception handler
|
|
|
|
|
*
|
|
|
|
|
* @param \Exception $exception
|
|
|
|
|
*/
|
2013-07-05 13:28:10 -04:00
|
|
|
public static function onException($exception) {
|
2015-05-19 06:35:22 -04:00
|
|
|
$class = get_class($exception);
|
|
|
|
|
$msg = $exception->getMessage();
|
|
|
|
|
$msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine();
|
|
|
|
|
self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']);
|
2013-07-05 13:28:10 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Recoverable errors handler
|
|
|
|
|
public static function onError($number, $message, $file, $line) {
|
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)) {
|
2013-07-05 13:28:10 -04:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$msg = $message . ' at ' . $file . '#' . $line;
|
2020-09-29 02:54:53 -04:00
|
|
|
$e = new \Error(self::removePassword($msg));
|
2021-04-26 08:18:18 -04:00
|
|
|
self::$logger->logException($e, ['app' => 'PHP', 'level' => self::errnoToLogLevel($number)]);
|
2014-01-31 07:27:51 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Recoverable handler which catch all errors, warnings and notices
|
|
|
|
|
public static function onAll($number, $message, $file, $line) {
|
|
|
|
|
$msg = $message . ' at ' . $file . '#' . $line;
|
2020-09-29 02:54:53 -04:00
|
|
|
$e = new \Error(self::removePassword($msg));
|
2021-04-26 08:18:18 -04:00
|
|
|
self::$logger->logException($e, ['app' => 'PHP', 'level' => self::errnoToLogLevel($number)]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function errnoToLogLevel(int $errno): int {
|
|
|
|
|
switch ($errno) {
|
|
|
|
|
case E_USER_WARNING:
|
|
|
|
|
return ILogger::WARN;
|
|
|
|
|
|
|
|
|
|
case E_USER_DEPRECATED:
|
2021-04-26 10:19:03 -04:00
|
|
|
return ILogger::DEBUG;
|
|
|
|
|
|
2021-04-26 08:18:18 -04:00
|
|
|
case E_USER_NOTICE:
|
|
|
|
|
return ILogger::INFO;
|
|
|
|
|
|
|
|
|
|
case E_USER_ERROR:
|
|
|
|
|
default:
|
|
|
|
|
return ILogger::ERROR;
|
|
|
|
|
}
|
2013-07-05 13:28:10 -04:00
|
|
|
}
|
|
|
|
|
}
|