errorhandler.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. } else {
  43. set_error_handler(array($handler, 'onError'));
  44. }
  45. register_shutdown_function(array($handler, 'onShutdown'));
  46. set_exception_handler(array($handler, 'onException'));
  47. }
  48. public static function setLogger(ILogger $logger) {
  49. self::$logger = $logger;
  50. }
  51. //Fatal errors handler
  52. public static function onShutdown() {
  53. $error = error_get_last();
  54. if($error && self::$logger) {
  55. //ob_end_clean();
  56. $msg = $error['message'] . ' at ' . $error['file'] . '#' . $error['line'];
  57. self::$logger->critical(self::removePassword($msg), array('app' => 'PHP'));
  58. }
  59. }
  60. /**
  61. * Uncaught exception handler
  62. *
  63. * @param \Exception $exception
  64. */
  65. public static function onException($exception) {
  66. $class = get_class($exception);
  67. $msg = $exception->getMessage();
  68. $msg = "$class: $msg at " . $exception->getFile() . '#' . $exception->getLine();
  69. self::$logger->critical(self::removePassword($msg), ['app' => 'PHP']);
  70. }
  71. //Recoverable errors handler
  72. public static function onError($number, $message, $file, $line) {
  73. if (error_reporting() === 0) {
  74. return;
  75. }
  76. $msg = $message . ' at ' . $file . '#' . $line;
  77. self::$logger->error(self::removePassword($msg), array('app' => 'PHP'));
  78. }
  79. //Recoverable handler which catch all errors, warnings and notices
  80. public static function onAll($number, $message, $file, $line) {
  81. $msg = $message . ' at ' . $file . '#' . $line;
  82. self::$logger->debug(self::removePassword($msg), array('app' => 'PHP'));
  83. }
  84. }