exceptionloggerplugin.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * @author Morris Jobke <hey@morrisjobke.de>
  4. * @author Thomas Müller <thomas.mueller@tmit.eu>
  5. * @author Vincent Petry <pvince81@owncloud.com>
  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\Connector\Sabre;
  24. use OCP\ILogger;
  25. use Sabre\DAV\Exception;
  26. use Sabre\HTTP\Response;
  27. class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin {
  28. protected $nonFatalExceptions = array(
  29. 'Sabre\DAV\Exception\NotAuthenticated' => true,
  30. // the sync client uses this to find out whether files exist,
  31. // so it is not always an error, log it as debug
  32. 'Sabre\DAV\Exception\NotFound' => true,
  33. // this one mostly happens when the same file is uploaded at
  34. // exactly the same time from two clients, only one client
  35. // wins, the second one gets "Precondition failed"
  36. 'Sabre\DAV\Exception\PreconditionFailed' => true,
  37. // forbidden can be expected when trying to upload to
  38. // read-only folders for example
  39. 'Sabre\DAV\Exception\Forbidden' => true,
  40. );
  41. /** @var string */
  42. private $appName;
  43. /** @var ILogger */
  44. private $logger;
  45. /**
  46. * @param string $loggerAppName app name to use when logging
  47. * @param ILogger $logger
  48. */
  49. public function __construct($loggerAppName, $logger) {
  50. $this->appName = $loggerAppName;
  51. $this->logger = $logger;
  52. }
  53. /**
  54. * This initializes the plugin.
  55. *
  56. * This function is called by \Sabre\DAV\Server, after
  57. * addPlugin is called.
  58. *
  59. * This method should set up the required event subscriptions.
  60. *
  61. * @param \Sabre\DAV\Server $server
  62. * @return void
  63. */
  64. public function initialize(\Sabre\DAV\Server $server) {
  65. $server->on('exception', array($this, 'logException'), 10);
  66. }
  67. /**
  68. * Log exception
  69. *
  70. */
  71. public function logException(\Exception $ex) {
  72. $exceptionClass = get_class($ex);
  73. $level = \OCP\Util::FATAL;
  74. if (isset($this->nonFatalExceptions[$exceptionClass])) {
  75. $level = \OCP\Util::DEBUG;
  76. }
  77. $message = $ex->getMessage();
  78. if ($ex instanceof Exception) {
  79. if (empty($message)) {
  80. $response = new Response($ex->getHTTPCode());
  81. $message = $response->getStatusText();
  82. }
  83. $message = "HTTP/1.1 {$ex->getHTTPCode()} $message";
  84. }
  85. $exception = [
  86. 'Message' => $message,
  87. 'Exception' => $exceptionClass,
  88. 'Code' => $ex->getCode(),
  89. 'Trace' => $ex->getTraceAsString(),
  90. 'File' => $ex->getFile(),
  91. 'Line' => $ex->getLine(),
  92. ];
  93. $this->logger->log($level, 'Exception: ' . json_encode($exception), ['app' => $this->appName]);
  94. }
  95. }