Skip to content

Commit ba2ed1a

Browse files
committed
Logs refactoring, logable response headers
1 parent c7e8bbd commit ba2ed1a

File tree

6 files changed

+75
-46
lines changed

6 files changed

+75
-46
lines changed

docs/configuration/settingBundle.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,18 @@ hb_webservice_core_async:
1111
1212
### Possible variants of configuration params in Settings Bundle
1313
14-
| Key | Value |
15-
|-----------------------------------------------------------|-----------------------|
16-
| `my_service/base_url` | `http://example.com` |
17-
| OVERRIDE`my_service/my_subService/base_url` | `http://example2.com` |
18-
| `cache/my_service/customAction/ttl` | 600 |
19-
| IF NO CUSTOM ACTION`cache/my_service/action/ttl` | 600 |
14+
| Key | Value |
15+
|----------------------------------------------------------|-----------------------|
16+
| `my_service/base_url` | `http://example.com` |
17+
| OVERRIDE`my_service/my_subService/base_url` | `http://example2.com` |
18+
| `cache/my_service/customAction/ttl` | 600 |
19+
| IF NO CUSTOM ACTION`cache/my_service/action/ttl` | 600 |
2020
| OVERRIDE`cache/my_service/my_subService/customAction/ttl` | 300 |
21-
| `timeout/my_service/customAction` | 15 |
22-
| OVERRIDE`timeout/my_service/my_subService/customAction` | 25 |
23-
| `logs/store` | 1 |
24-
| OVERRIDE`logs/store/customAction` | 0 |
25-
| `logs/mask_sensitive_data` | 1 |
26-
| `logs/mask_sensitive_member_pii` | 1 |
27-
| `logs/max_length` | 900000 |
21+
| `timeout/my_service/customAction` | 15 |
22+
| OVERRIDE`timeout/my_service/my_subService/customAction` | 25 |
23+
| `logs/store` | 1 |
24+
| OVERRIDE`logs/store/customAction` | 0 |
25+
| `logs/mask_sensitive_data` | 1 |
26+
| `logs/mask_sensitive_member_pii` | 1 |
27+
| `logs/log_response_headers` | 1 |
28+
| `logs/max_length` | 900000 |

docs/configuration/symfonyParams.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,6 @@ parameters:
2323
hb_webservice_core_async.logs.store.customAction: 0
2424
hb_webservice_core_async.logs.mask_sensitive_data: 1
2525
hb_webservice_core_async.logs.mask_sensitive_member_pii: 1
26+
hb_webservice_core_async.logs.log_response_headers: 1
2627
hb_webservice_core_async.logs.max_length: 900000
2728
```

src/Logs/MonologLogHandler.php

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -45,42 +45,26 @@ public function writeLog(ParsedResponse $parsedResponse): void
4545
$this->maskSensitiveMemberPII = (bool)$this->paramsProvider->getLogParameterValue('mask_sensitive_member_pii');
4646
}
4747

48-
$requestStringParts = [];
49-
$requestOptions = $WSRequest->getOptions();
50-
foreach (['query', 'json', 'body'] as $field) {
51-
if (!empty($requestOptions[$field])) {
52-
$value = $requestOptions[$field];
53-
if ($field === 'body' && !empty($value['body'])) {
54-
$value = $value['body'];
55-
}
56-
try {
57-
$requestStringParts[] = is_string($value)
58-
? $value
59-
: json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
60-
} catch (\JsonException $e) {
61-
$requestStringParts[] = $e->getMessage();
62-
}
63-
}
64-
}
65-
$requestString = implode("\n\n", $requestStringParts);
66-
67-
$responseBody = $parsedResponse->responseBody;
48+
$requestString = $WSRequest->getLogString();
49+
$responseString = $parsedResponse->getLogString(
50+
(bool)$this->paramsProvider->getLogParameterValue('log_response_headers')
51+
);
6852
if ($this->maskSensitiveData) {
6953
MaskLogHelper::maskSensitiveVar($requestString, $this->maskSensitiveMemberPII);
70-
MaskLogHelper::maskSensitiveVar($responseBody, true);
54+
MaskLogHelper::maskSensitiveVar($responseString, true);
7155
}
7256
$responseMaxLength = $this->paramsProvider->getLogParameterValue('max_length', 900000);
73-
if ($responseBody && strlen($responseBody) > $responseMaxLength) {
74-
$response = substr($responseBody, 0, $responseMaxLength);
75-
$responseBody = $response;
57+
if ($responseString && strlen($responseString) > $responseMaxLength) {
58+
$response = substr($responseString, 0, $responseMaxLength);
59+
$responseString = $response;
7660
}
7761

7862
$logContext = [
7963
'service' => $webservice,
8064
'action' => $WSRequest->getCustomAction(),
8165
'clientip' => $currentRequest?->getClientIp(),
8266
'request' => $requestString,
83-
'response' => $responseBody,
67+
'response' => $responseString,
8468
'error' => $parsedResponse->exception?->getMessage(),
8569
'duration' => $parsedResponse->mainAsyncResponse->WSResponse->getInfo('total_time'),
8670
'uri' => $WSRequest->action,

src/Provider/ParamsProvider/SettingsBundleParamsProvider.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ public function __construct(private SettingHandlerInterface $settingHandler)
1515
public function getCacheTTL(WSRequest $request): ?int
1616
{
1717
if ($request->subService) {
18-
$ttl = (int)$this->settingHandler->get(
19-
'cache/' . $request->webService . '/' . $request->subService . '/' . $request->getCustomAction() . '/ttl', '0'
18+
$ttl = $this->settingHandler->get(
19+
'cache/' . $request->webService . '/' . $request->subService . '/' . $request->getCustomAction() . '/ttl'
2020
);
2121
}
2222

23-
$ttl ??= (int)$this->settingHandler->get(
24-
'cache/' . $request->webService . '/' . $request->getCustomAction() . '/ttl', '0'
23+
$ttl ??= $this->settingHandler->get(
24+
'cache/' . $request->webService . '/' . $request->getCustomAction() . '/ttl'
2525
);
2626

27-
return $ttl;
27+
return $ttl ? (int)$ttl : null;
2828
}
2929

3030
public function getBaseURL(WSRequest $request): ?string

src/Request/WSRequest.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,37 @@ public function setCacheVaryingParams(array $params): static
167167

168168
public function getCacheParams(): array
169169
{
170-
$requestParams = $this->getRequestParams();
170+
$query = $this->options['query'] ?? [];
171+
$body = $this->options['body'] ?? [];
172+
$json = $this->options['json'] ?? [];
173+
ksort($query);
174+
ksort($body);
175+
ksort($json);
176+
177+
$requestParams = [$query, $body, $json];
171178
$varyingParams = $this->cacheVaryingParams;
172-
ksort($requestParams);
173179
ksort($varyingParams);
174180

175181
return array_merge($requestParams, $varyingParams);
176182
}
183+
184+
public function getLogString(): string
185+
{
186+
$requestStringParts = [];
187+
$requestOptions = $this->getOptions();
188+
foreach (['query', 'json', 'body'] as $field) {
189+
if (!empty($requestOptions[$field])) {
190+
$value = $requestOptions[$field];
191+
try {
192+
$requestStringParts[] = is_string($value)
193+
? $value
194+
: json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR);
195+
} catch (\JsonException $e) {
196+
$requestStringParts[] = $e->getMessage();
197+
}
198+
}
199+
}
200+
201+
return implode("\n\n", $requestStringParts);
202+
}
177203
}

src/Response/ParsedResponse.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,21 @@ public function __construct(public readonly AsyncResponse $mainAsyncResponse)
1616
{
1717
}
1818

19-
}
19+
public function getLogString(bool $withHeaders = false): string
20+
{
21+
$responseInfo = $this->statusCode;
22+
if ($withHeaders && $this->headers) {
23+
try {
24+
$responseInfo .= " - "
25+
. json_encode(
26+
$this->headers,
27+
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR
28+
);
29+
} catch (\JsonException $e) {
30+
$responseInfo .= " - " . $e->getMessage();
31+
}
32+
}
33+
34+
return $responseInfo . "\n\n" . $this->responseBody;
35+
}
36+
}

0 commit comments

Comments
 (0)