set log for test shayegan

This commit is contained in:
2026-10-17 10:28:01 +03:30
parent f1c68d955d
commit 5b431c64a6
2 changed files with 79 additions and 3 deletions
+78 -3
View File
@@ -124,6 +124,14 @@ class ShayeganClient
$url = $this->endpointUrl($endpoint, $query);
$timeout = max(5, $this->config->getInt('shayegan_timeout_seconds', 30));
$this->logTraffic('request', [
'style' => $style,
'endpoint' => $endpoint,
'url' => $url,
'timeout_seconds' => $timeout,
'payload' => $this->redactPayload($payload),
]);
try {
$response = Http::timeout($timeout)
->withHeaders(['Content-Type' => 'application/json', 'Accept' => 'application/json'])
@@ -139,34 +147,69 @@ class ShayeganClient
$error = $this->extractErrorMessage($json);
if ($status >= 400) {
return [
$result = [
'ok' => false,
'message' => $error !== '' ? $error : ('HTTP ' . $status),
'http_status' => $status,
'body' => $json,
];
$this->logTraffic('response', [
'style' => $style,
'endpoint' => $endpoint,
'url' => $url,
'ok' => false,
'http_status' => $status,
'error_message' => $error,
'body' => $this->redactBodyForLog($json),
]);
return $result;
}
if ($error !== '') {
return [
$result = [
'ok' => false,
'message' => $error,
'http_status' => $status,
'body' => $json,
];
$this->logTraffic('response', [
'style' => $style,
'endpoint' => $endpoint,
'url' => $url,
'ok' => false,
'http_status' => $status,
'error_message' => $error,
'body' => $this->redactBodyForLog($json),
]);
return $result;
}
return [
$result = [
'ok' => true,
'message' => 'OK',
'http_status' => $status,
'body' => $json,
];
$this->logTraffic('response', [
'style' => $style,
'endpoint' => $endpoint,
'url' => $url,
'ok' => true,
'http_status' => $status,
'error_message' => '',
'body' => $this->redactBodyForLog($json),
]);
return $result;
} catch (\Illuminate\Http\Client\ConnectionException $e) {
Log::warning('shayegan.timeout_or_connection', [
'endpoint' => $endpoint,
'style' => $style,
'url' => $url,
'message' => $e->getMessage(),
'payload' => $this->redactPayload($payload),
]);
return [
@@ -179,7 +222,10 @@ class ShayeganClient
} catch (\Throwable $e) {
Log::warning('shayegan.request_failed', [
'endpoint' => $endpoint,
'style' => $style,
'url' => $url,
'message' => $e->getMessage(),
'payload' => $this->redactPayload($payload),
]);
return [
@@ -263,4 +309,33 @@ class ShayeganClient
return $payload;
}
/**
* @param array<string, mixed> $context
*/
private function logTraffic(string $phase, array $context): void
{
if ($this->config->getInt('shayegan_log_traffic', 1) === 0) {
return;
}
Log::info('shayegan.http_' . $phase, $context);
}
/**
* @param array<string, mixed> $body
* @return array<string, mixed>
*/
private function redactBodyForLog(array $body): array
{
if (isset($body['Result']) && is_array($body['Result'])) {
$count = count($body['Result']);
$body['Result'] = array_slice($body['Result'], 0, 2);
if ($count > 2) {
$body['Result_truncated'] = $count - 2;
}
}
return $body;
}
}