ErrorHandler.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. private LoggerInterface $logger;
  36. public function __construct(LoggerInterface $logger) {
  37. $this->logger = $logger;
  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. switch ($errno) {
  87. case E_USER_WARNING:
  88. return ILogger::WARN;
  89. case E_DEPRECATED:
  90. case E_USER_DEPRECATED:
  91. return ILogger::DEBUG;
  92. case E_USER_NOTICE:
  93. return ILogger::INFO;
  94. case E_USER_ERROR:
  95. default:
  96. return ILogger::ERROR;
  97. }
  98. }
  99. }