Skip to content

Commit

Permalink
Logger - add headers into log file
Browse files Browse the repository at this point in the history
  • Loading branch information
pekral committed Oct 13, 2020
1 parent c76f3ab commit 93218b0
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Http/HttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function curl_getinfo;
use function date;
use function dirname;
use function implode;
use function is_string;
use function mb_strlen;
use function microtime;
Expand Down Expand Up @@ -73,15 +74,22 @@ public function request(
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($config->getLogFilePath() !== null) {
$headersContents = [];

foreach ($headers as $key => $value) {
$headersContents[] = sprintf('%s:%s', $key, $value);
}

$rootDir = dirname($config->getLogFilePath());
$fileSystem = new Filesystem(new Local($rootDir, FILE_APPEND));
$logContent = sprintf(
'%s METHOD: %s URL:%s TIME:%s STATUS:%s',
'%s METHOD: %s URL:%s TIME:%s STATUS:%s HEADERS: %s',
date('Y-m-d H:i:s'),
$httpMethod->getValue(),
$url,
number_format($responseTime, 2),
$statusCode,
implode(',', $headersContents),
);

if ($errorMessage !== null) {
Expand Down
1 change: 1 addition & 0 deletions tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public function testLogRequest(): void

Assert::assertTrue(file_exists($logPath));
Assert::assertNotEmpty(file_get_contents($logPath));
unlink($logPath);
}

private function checkResponseStructure(EvidenceResult $result): void
Expand Down
1 change: 0 additions & 1 deletion tests/logs/log.txt

This file was deleted.

0 comments on commit 93218b0

Please sign in to comment.