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