You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.2 KiB

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