LogDetails.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /** @var SystemConfig */
  30. private $config;
  31. public function __construct(SystemConfig $config) {
  32. $this->config = $config;
  33. }
  34. public function logDetails(string $app, $message, int $level): array {
  35. // default to ISO8601
  36. $format = $this->config->getValue('logdateformat', \DateTimeInterface::ATOM);
  37. $logTimeZone = $this->config->getValue('logtimezone', 'UTC');
  38. try {
  39. $timezone = new \DateTimeZone($logTimeZone);
  40. } catch (\Exception $e) {
  41. $timezone = new \DateTimeZone('UTC');
  42. }
  43. $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""));
  44. if ($time === false) {
  45. $time = new \DateTime('now', $timezone);
  46. } else {
  47. // apply timezone if $time is created from UNIX timestamp
  48. $time->setTimezone($timezone);
  49. }
  50. $request = \OC::$server->getRequest();
  51. $reqId = $request->getId();
  52. $remoteAddr = $request->getRemoteAddress();
  53. // remove username/passwords from URLs before writing the to the log file
  54. $time = $time->format($format);
  55. $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
  56. $method = is_string($request->getMethod()) ? $request->getMethod() : '--';
  57. if ($this->config->getValue('installed', false)) {
  58. $user = \OC_User::getUser() ? \OC_User::getUser() : '--';
  59. } else {
  60. $user = '--';
  61. }
  62. $userAgent = $request->getHeader('User-Agent');
  63. if ($userAgent === '') {
  64. $userAgent = '--';
  65. }
  66. $version = $this->config->getValue('version', '');
  67. $entry = compact(
  68. 'reqId',
  69. 'level',
  70. 'time',
  71. 'remoteAddr',
  72. 'user',
  73. 'app',
  74. 'method',
  75. 'url',
  76. 'message',
  77. 'userAgent',
  78. 'version'
  79. );
  80. if (is_array($message)) {
  81. // Exception messages are extracted and the exception is put into a separate field
  82. // anything else modern is split to 'message' (string) and
  83. // data (array) fields
  84. if (array_key_exists('Exception', $message)) {
  85. $entry['exception'] = $message;
  86. $entry['message'] = $message['CustomMessage'] !== '--' ? $message['CustomMessage'] : $message['Message'];
  87. } else {
  88. $entry['message'] = $message['message'] ?? '(no message provided)';
  89. unset($message['message']);
  90. $entry['data'] = $message;
  91. }
  92. }
  93. return $entry;
  94. }
  95. public function logDetailsAsJSON(string $app, $message, int $level): string {
  96. $entry = $this->logDetails($app, $message, $level);
  97. // PHP's json_encode only accept proper UTF-8 strings, loop over all
  98. // elements to ensure that they are properly UTF-8 compliant or convert
  99. // them manually.
  100. foreach ($entry as $key => $value) {
  101. if (is_string($value)) {
  102. $testEncode = json_encode($value, JSON_UNESCAPED_SLASHES);
  103. if ($testEncode === false) {
  104. $entry[$key] = mb_convert_encoding($value, 'UTF-8', mb_detect_encoding($value));
  105. }
  106. }
  107. }
  108. return json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
  109. }
  110. }