ExceptionLoggerPlugin.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Pierre Jochem <pierrejochem@msn.com>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. * @author Thomas Müller <thomas.mueller@tmit.eu>
  10. * @author Vincent Petry <pvince81@owncloud.com>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Connector\Sabre;
  28. use OCP\ILogger;
  29. use Sabre\DAV\Exception;
  30. use Sabre\HTTP\Response;
  31. class ExceptionLoggerPlugin extends \Sabre\DAV\ServerPlugin {
  32. protected $nonFatalExceptions = [
  33. 'Sabre\DAV\Exception\NotAuthenticated' => true,
  34. // If tokenauth can throw this exception (which is basically as
  35. // NotAuthenticated. So not fatal.
  36. 'OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden' => true,
  37. // the sync client uses this to find out whether files exist,
  38. // so it is not always an error, log it as debug
  39. 'Sabre\DAV\Exception\NotFound' => true,
  40. // this one mostly happens when the same file is uploaded at
  41. // exactly the same time from two clients, only one client
  42. // wins, the second one gets "Precondition failed"
  43. 'Sabre\DAV\Exception\PreconditionFailed' => true,
  44. // forbidden can be expected when trying to upload to
  45. // read-only folders for example
  46. 'Sabre\DAV\Exception\Forbidden' => true,
  47. // Happens when an external storage or federated share is temporarily
  48. // not available
  49. 'Sabre\DAV\Exception\StorageNotAvailableException' => true,
  50. ];
  51. /** @var string */
  52. private $appName;
  53. /** @var ILogger */
  54. private $logger;
  55. /**
  56. * @param string $loggerAppName app name to use when logging
  57. * @param ILogger $logger
  58. */
  59. public function __construct($loggerAppName, $logger) {
  60. $this->appName = $loggerAppName;
  61. $this->logger = $logger;
  62. }
  63. /**
  64. * This initializes the plugin.
  65. *
  66. * This function is called by \Sabre\DAV\Server, after
  67. * addPlugin is called.
  68. *
  69. * This method should set up the required event subscriptions.
  70. *
  71. * @param \Sabre\DAV\Server $server
  72. * @return void
  73. */
  74. public function initialize(\Sabre\DAV\Server $server) {
  75. $server->on('exception', array($this, 'logException'), 10);
  76. }
  77. /**
  78. * Log exception
  79. *
  80. */
  81. public function logException(\Exception $ex) {
  82. $exceptionClass = get_class($ex);
  83. $level = \OCP\Util::FATAL;
  84. if (isset($this->nonFatalExceptions[$exceptionClass])) {
  85. $level = \OCP\Util::DEBUG;
  86. }
  87. $message = $ex->getMessage();
  88. if ($ex instanceof Exception) {
  89. if (empty($message)) {
  90. $response = new Response($ex->getHTTPCode());
  91. $message = $response->getStatusText();
  92. }
  93. $message = "HTTP/1.1 {$ex->getHTTPCode()} $message";
  94. }
  95. $user = \OC_User::getUser();
  96. $exception = [
  97. 'Message' => $message,
  98. 'Exception' => $exceptionClass,
  99. 'Code' => $ex->getCode(),
  100. 'Trace' => $ex->getTraceAsString(),
  101. 'File' => $ex->getFile(),
  102. 'Line' => $ex->getLine(),
  103. 'User' => $user,
  104. ];
  105. $this->logger->log($level, 'Exception: ' . json_encode($exception), ['app' => $this->appName]);
  106. }
  107. }