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