123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- namespace OC\Log;
- use OC\SystemConfig;
- abstract class LogDetails {
- public function __construct(
- private SystemConfig $config,
- ) {
- }
- public function logDetails(string $app, $message, int $level): array {
-
- $format = $this->config->getValue('logdateformat', \DateTimeInterface::ATOM);
- $logTimeZone = $this->config->getValue('logtimezone', 'UTC');
- try {
- $timezone = new \DateTimeZone($logTimeZone);
- } catch (\Exception $e) {
- $timezone = new \DateTimeZone('UTC');
- }
- $time = \DateTime::createFromFormat("U.u", number_format(microtime(true), 4, ".", ""));
- if ($time === false) {
- $time = new \DateTime('now', $timezone);
- } else {
-
- $time->setTimezone($timezone);
- }
- $request = \OC::$server->getRequest();
- $reqId = $request->getId();
- $remoteAddr = $request->getRemoteAddress();
-
- $time = $time->format($format);
- $url = ($request->getRequestUri() !== '') ? $request->getRequestUri() : '--';
- $method = is_string($request->getMethod()) ? $request->getMethod() : '--';
- if ($this->config->getValue('installed', false)) {
- $user = \OC_User::getUser() ? \OC_User::getUser() : '--';
- } else {
- $user = '--';
- }
- $userAgent = $request->getHeader('User-Agent');
- if ($userAgent === '') {
- $userAgent = '--';
- }
- $version = $this->config->getValue('version', '');
- $entry = compact(
- 'reqId',
- 'level',
- 'time',
- 'remoteAddr',
- 'user',
- 'app',
- 'method',
- 'url',
- 'message',
- 'userAgent',
- 'version'
- );
- if (is_array($message)) {
-
-
-
- if (array_key_exists('Exception', $message)) {
- $entry['exception'] = $message;
- $entry['message'] = $message['CustomMessage'] !== '--' ? $message['CustomMessage'] : $message['Message'];
- } else {
- $entry['message'] = $message['message'] ?? '(no message provided)';
- unset($message['message']);
- $entry['data'] = $message;
- }
- }
- return $entry;
- }
- public function logDetailsAsJSON(string $app, $message, int $level): string {
- $entry = $this->logDetails($app, $message, $level);
-
-
-
- foreach ($entry as $key => $value) {
- if (is_string($value)) {
- $testEncode = json_encode($value, JSON_UNESCAPED_SLASHES);
- if ($testEncode === false) {
- $entry[$key] = mb_convert_encoding($value, 'UTF-8', mb_detect_encoding($value));
- }
- }
- }
- return json_encode($entry, JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES);
- }
- }
|