errorhandler.php 2.7 KB

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