errorhandler.php 2.8 KB

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