1
0

LogDetails.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Log;
  7. use OC\SystemConfig;
  8. abstract class LogDetails {
  9. public function __construct(
  10. private SystemConfig $config,
  11. ) {
  12. }
  13. public function logDetails(string $app, $message, int $level): array {
  14. // default to ISO8601
  15. $format = $this->config->getValue('logdateformat', \DateTimeInterface::ATOM);
  16. $logTimeZone = $this->config->getValue('logtimezone', 'UTC');
  17. try {
  18. $timezone = new \DateTimeZone($logTimeZone);
  19. } catch (\Exception $e) {
  20. $timezone = new \DateTimeZone('UTC');
  21. }
  22. $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""));
  23. if ($time === false) {
  24. $time = new \DateTime('now', $timezone);
  25. } else {
  26. // apply timezone if $time is created from UNIX timestamp
  27. $time->setTimezone($timezone);
  28. }
  29. $request = \OC::$server->getRequest();
  30. $reqId = $request->getId();
  31. $remoteAddr = $request->getRemoteAddress();
  32. // remove username/passwords from URLs before writing the to the log file
  33. $time = $time->format($format);
  34. $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
  35. $method = is_string($request->getMethod()) ? $request->getMethod() : '--';
  36. if ($this->config->getValue('installed', false)) {
  37. $user = \OC_User::getUser() ? \OC_User::getUser() : '--';
  38. } else {
  39. $user = '--';
  40. }
  41. $userAgent = $request->getHeader('User-Agent');
  42. if ($userAgent === '') {
  43. $userAgent = '--';
  44. }
  45. $version = $this->config->getValue('version', '');
  46. $entry = compact(
  47. 'reqId',
  48. 'level',
  49. 'time',
  50. 'remoteAddr',
  51. 'user',
  52. 'app',
  53. 'method',
  54. 'url',
  55. 'message',
  56. 'userAgent',
  57. 'version'
  58. );
  59. if (is_array($message)) {
  60. // Exception messages are extracted and the exception is put into a separate field
  61. // anything else modern is split to 'message' (string) and
  62. // data (array) fields
  63. if (array_key_exists('Exception', $message)) {
  64. $entry['exception'] = $message;
  65. $entry['message'] = $message['CustomMessage'] !== '--' ? $message['CustomMessage'] : $message['Message'];
  66. } else {
  67. $entry['message'] = $message['message'] ?? '(no message provided)';
  68. unset($message['message']);
  69. $entry['data'] = $message;
  70. }
  71. }
  72. return $entry;
  73. }
  74. public function logDetailsAsJSON(string $app, $message, int $level): string {
  75. $entry = $this->logDetails($app, $message, $level);
  76. // PHP's json_encode only accept proper UTF-8 strings, loop over all
  77. // elements to ensure that they are properly UTF-8 compliant or convert
  78. // them manually.
  79. foreach ($entry as $key => $value) {
  80. if (is_string($value)) {
  81. $testEncode = json_encode($value, JSON_UNESCAPED_SLASHES);
  82. if ($testEncode === false) {
  83. $entry[$key] = mb_convert_encoding($value, 'UTF-8', mb_detect_encoding($value));
  84. }
  85. }
  86. }
  87. return json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
  88. }
  89. }