Skip to content

Commit c7e8bbd

Browse files
committed
Log all request params instead of first found
1 parent a10e790 commit c7e8bbd

File tree

1 file changed

+29
-10
lines changed

1 file changed

+29
-10
lines changed

src/Logs/MonologLogHandler.php

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,49 @@ public function __construct(
2121

2222
public function writeLog(ParsedResponse $parsedResponse): void
2323
{
24+
$WSRequest = $parsedResponse->mainAsyncResponse->WSRequest;
25+
2426
if (
2527
!$this->paramsProvider->getLogParameterValue('store', '1')
2628
|| !$this->paramsProvider->getLogParameterValue(
27-
'store/' . $parsedResponse->mainAsyncResponse->WSRequest->getCustomAction(), '1'
29+
'store/' . $WSRequest->getCustomAction(), '1'
2830
)
2931
) {
3032
return;
3133
}
3234

3335
$currentRequest = $this->requestStack->getCurrentRequest();
34-
$webservice = $parsedResponse->mainAsyncResponse->WSRequest->webService;
35-
if ($parsedResponse->mainAsyncResponse->WSRequest->subService) {
36-
$webservice .= '-' . $parsedResponse->mainAsyncResponse->WSRequest->subService;
36+
$webservice = $WSRequest->webService;
37+
if ($WSRequest->subService) {
38+
$webservice .= '-' . $WSRequest->subService;
3739
}
3840

39-
$requestParams = $parsedResponse->mainAsyncResponse->WSRequest->getRequestParams();
40-
4141
if ($this->maskSensitiveData === null) {
4242
$this->maskSensitiveData = (bool)$this->paramsProvider->getLogParameterValue('mask_sensitive_data');
4343
}
4444
if ($this->maskSensitiveMemberPII === null) {
4545
$this->maskSensitiveMemberPII = (bool)$this->paramsProvider->getLogParameterValue('mask_sensitive_member_pii');
4646
}
47-
$requestString = json_encode($requestParams, JSON_THROW_ON_ERROR);
47+
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+
4867
$responseBody = $parsedResponse->responseBody;
4968
if ($this->maskSensitiveData) {
5069
MaskLogHelper::maskSensitiveVar($requestString, $this->maskSensitiveMemberPII);
@@ -58,13 +77,13 @@ public function writeLog(ParsedResponse $parsedResponse): void
5877

5978
$logContext = [
6079
'service' => $webservice,
61-
'action' => $parsedResponse->mainAsyncResponse->WSRequest->getCustomAction(),
80+
'action' => $WSRequest->getCustomAction(),
6281
'clientip' => $currentRequest?->getClientIp(),
6382
'request' => $requestString,
6483
'response' => $responseBody,
6584
'error' => $parsedResponse->exception?->getMessage(),
6685
'duration' => $parsedResponse->mainAsyncResponse->WSResponse->getInfo('total_time'),
67-
'uri' => $parsedResponse->mainAsyncResponse->WSRequest->action,
86+
'uri' => $WSRequest->action,
6887
];
6988

7089
if ($logContext['error'] !== null) {
@@ -73,4 +92,4 @@ public function writeLog(ParsedResponse $parsedResponse): void
7392
$this->logger->info('CL', $logContext);
7493
}
7594
}
76-
}
95+
}

0 commit comments

Comments
 (0)