You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ErrorHandler.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OC\Log;
  30. use Error;
  31. use OCP\ILogger;
  32. use Psr\Log\LoggerInterface;
  33. use Throwable;
  34. class ErrorHandler {
  35. public function __construct(
  36. private LoggerInterface $logger,
  37. ) {
  38. }
  39. /**
  40. * Remove password in URLs
  41. */
  42. private static function removePassword(string $msg): string {
  43. return preg_replace('#//(.*):(.*)@#', '//xxx:xxx@', $msg);
  44. }
  45. /**
  46. * Fatal errors handler
  47. */
  48. public function onShutdown(): void {
  49. $error = error_get_last();
  50. if ($error) {
  51. $msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line'];
  52. $this->logger->critical(self::removePassword($msg), ['app' => 'PHP']);
  53. }
  54. }
  55. /**
  56. * Uncaught exception handler
  57. */
  58. public function onException(Throwable $exception): void {
  59. $class = get_class($exception);
  60. $msg = $exception->getMessage();
  61. $msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine();
  62. $this->logger->critical(self::removePassword($msg), ['app' => 'PHP']);
  63. }
  64. /**
  65. * Recoverable errors handler
  66. */
  67. public function onError(int $number, string $message, string $file, int $line): bool {
  68. if (!(error_reporting() & $number)) {
  69. return true;
  70. }
  71. $msg = $message . ' at ' . $file . '#' . $line;
  72. $e = new Error(self::removePassword($msg));
  73. $this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']);
  74. return true;
  75. }
  76. /**
  77. * Recoverable handler which catch all errors, warnings and notices
  78. */
  79. public function onAll(int $number, string $message, string $file, int $line): bool {
  80. $msg = $message . ' at ' . $file . '#' . $line;
  81. $e = new Error(self::removePassword($msg));
  82. $this->logger->log(self::errnoToLogLevel($number), $e->getMessage(), ['app' => 'PHP']);
  83. return true;
  84. }
  85. private static function errnoToLogLevel(int $errno): int {
  86. return match ($errno) {
  87. E_USER_WARNING => ILogger::WARN,
  88. E_DEPRECATED, E_USER_DEPRECATED => ILogger::DEBUG,
  89. E_USER_NOTICE => ILogger::INFO,
  90. default => ILogger::ERROR,
  91. };
  92. }
  93. }