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.

101 lines
3.0 KiB

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