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.

113 lines
3.5 KiB

3 years ago
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2019 Julius Härtl <jus@bitgrid.net>
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Julius Härtl <jus@bitgrid.net>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  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
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Log;
  26. use OC\SystemConfig;
  27. abstract class LogDetails {
  28. /** @var SystemConfig */
  29. private $config;
  30. public function __construct(SystemConfig $config) {
  31. $this->config = $config;
  32. }
  33. public function logDetails(string $app, $message, int $level): array {
  34. // default to ISO8601
  35. $format = $this->config->getValue('logdateformat', \DateTime::ATOM);
  36. $logTimeZone = $this->config->getValue('logtimezone', 'UTC');
  37. try {
  38. $timezone = new \DateTimeZone($logTimeZone);
  39. } catch (\Exception $e) {
  40. $timezone = new \DateTimeZone('UTC');
  41. }
  42. $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""));
  43. if ($time === false) {
  44. $time = new \DateTime(null, $timezone);
  45. } else {
  46. // apply timezone if $time is created from UNIX timestamp
  47. $time->setTimezone($timezone);
  48. }
  49. $request = \OC::$server->getRequest();
  50. $reqId = $request->getId();
  51. $remoteAddr = $request->getRemoteAddress();
  52. // remove username/passwords from URLs before writing the to the log file
  53. $time = $time->format($format);
  54. $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
  55. $method = is_string($request->getMethod()) ? $request->getMethod() : '--';
  56. if ($this->config->getValue('installed', false)) {
  57. $user = \OC_User::getUser() ? \OC_User::getUser() : '--';
  58. } else {
  59. $user = '--';
  60. }
  61. $userAgent = $request->getHeader('User-Agent');
  62. if ($userAgent === '') {
  63. $userAgent = '--';
  64. }
  65. $version = $this->config->getValue('version', '');
  66. $entry = compact(
  67. 'reqId',
  68. 'level',
  69. 'time',
  70. 'remoteAddr',
  71. 'user',
  72. 'app',
  73. 'method',
  74. 'url',
  75. 'message',
  76. 'userAgent',
  77. 'version'
  78. );
  79. if (is_array($message) && !array_key_exists('Exception', $message)) {
  80. // Exception messages should stay as they are,
  81. // anything else modern is split to 'message' (string) and
  82. // data (array) fields
  83. $shortMessage = $message['message'] ?? '(no message provided)';
  84. $entry['data'] = $message;
  85. $entry['message'] = $shortMessage;
  86. }
  87. return $entry;
  88. }
  89. public function logDetailsAsJSON(string $app, $message, int $level): string {
  90. $entry = $this->logDetails($app, $message, $level);
  91. // PHP's json_encode only accept proper UTF-8 strings, loop over all
  92. // elements to ensure that they are properly UTF-8 compliant or convert
  93. // them manually.
  94. foreach ($entry as $key => $value) {
  95. if (is_string($value)) {
  96. $testEncode = json_encode($value, JSON_UNESCAPED_SLASHES);
  97. if ($testEncode === false) {
  98. $entry[$key] = utf8_encode($value);
  99. }
  100. }
  101. }
  102. return json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
  103. }
  104. }