ErrorHandler.php 2.9 KB

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