exceptionloggerplugin.php 3.1 KB

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