1
0

LogDetails.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. * @author Thomas Citharel <nextcloud@tcit.fr>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Log;
  27. use OC\SystemConfig;
  28. abstract class LogDetails {
  29. public function __construct(
  30. private SystemConfig $config,
  31. ) {
  32. }
  33. public function logDetails(string $app, $message, int $level): array {
  34. // default to ISO8601
  35. $format = $this->config->getValue('logdateformat', \DateTimeInterface::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('now', $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)) {
  80. // Exception messages are extracted and the exception is put into a separate field
  81. // anything else modern is split to 'message' (string) and
  82. // data (array) fields
  83. if (array_key_exists('Exception', $message)) {
  84. $entry['exception'] = $message;
  85. $entry['message'] = $message['CustomMessage'] !== '--' ? $message['CustomMessage'] : $message['Message'];
  86. } else {
  87. $entry['message'] = $message['message'] ?? '(no message provided)';
  88. unset($message['message']);
  89. $entry['data'] = $message;
  90. }
  91. }
  92. return $entry;
  93. }
  94. public function logDetailsAsJSON(string $app, $message, int $level): string {
  95. $entry = $this->logDetails($app, $message, $level);
  96. // PHP's json_encode only accept proper UTF-8 strings, loop over all
  97. // elements to ensure that they are properly UTF-8 compliant or convert
  98. // them manually.
  99. foreach ($entry as $key => $value) {
  100. if (is_string($value)) {
  101. $testEncode = json_encode($value, JSON_UNESCAPED_SLASHES);
  102. if ($testEncode === false) {
  103. $entry[$key] = utf8_encode($value);
  104. }
  105. }
  106. }
  107. return json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
  108. }
  109. }