-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #211 from laminas/feature/log-http-responses
Log HTTP requests/responses (only non-sensitive data)
- Loading branch information
Showing
13 changed files
with
371 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,6 +12,6 @@ | |
"mutators": { | ||
"@default": true | ||
}, | ||
"minMsi": 97, | ||
"minMsi": 98, | ||
"minCoveredMsi": 100 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\HttpClient; | ||
|
||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** @internal */ | ||
final class LoggingHttpClient implements ClientInterface | ||
{ | ||
public function __construct(private readonly ClientInterface $next, private readonly LoggerInterface $logger) | ||
{ | ||
} | ||
|
||
public function sendRequest(RequestInterface $request): ResponseInterface | ||
{ | ||
$this->logger->debug('Sending request {request}', ['request' => $request]); | ||
|
||
$response = $this->next->sendRequest($request); | ||
|
||
$this->logger->debug( | ||
'Received response {response} to request {request}', | ||
[ | ||
'request' => $request, | ||
'response' => $response, | ||
], | ||
); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\Monolog; | ||
|
||
use Monolog\LogRecord; | ||
use Monolog\Processor\ProcessorInterface; | ||
use Psr\Http\Message\RequestInterface; | ||
|
||
use function array_map; | ||
|
||
/** @internal */ | ||
final class ConvertLogContextHttpRequestsIntoStrings implements ProcessorInterface | ||
{ | ||
public function __invoke(LogRecord $record): LogRecord | ||
{ | ||
return new LogRecord( | ||
$record->datetime, | ||
$record->channel, | ||
$record->level, | ||
$record->message, | ||
array_map(self::contextItemToMessage(...), $record->context), | ||
$record->extra, | ||
$record->formatted, | ||
); | ||
} | ||
|
||
private static function contextItemToMessage(mixed $item): mixed | ||
{ | ||
if (! $item instanceof RequestInterface) { | ||
return $item; | ||
} | ||
|
||
return $item->getMethod() | ||
. ' ' | ||
. $item | ||
->getUri() | ||
->withUserInfo('') | ||
->__toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\Monolog; | ||
|
||
use Monolog\LogRecord; | ||
use Monolog\Processor\ProcessorInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
|
||
use function array_map; | ||
|
||
/** @internal */ | ||
final class ConvertLogContextHttpResponsesIntoStrings implements ProcessorInterface | ||
{ | ||
public function __invoke(LogRecord $record): LogRecord | ||
{ | ||
return new LogRecord( | ||
$record->datetime, | ||
$record->channel, | ||
$record->level, | ||
$record->message, | ||
array_map(self::contextItemToMessage(...), $record->context), | ||
$record->extra, | ||
$record->formatted, | ||
); | ||
} | ||
|
||
private static function contextItemToMessage(mixed $item): mixed | ||
{ | ||
if (! $item instanceof ResponseInterface) { | ||
return $item; | ||
} | ||
|
||
return $item->getStatusCode() | ||
. ' "' | ||
. $item | ||
->getBody() | ||
->__toString() | ||
. '"'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\Test\Unit\HttpClient; | ||
|
||
use Http\Discovery\Psr17FactoryDiscovery; | ||
use Laminas\AutomaticReleases\HttpClient\LoggingHttpClient; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
/** @covers \Laminas\AutomaticReleases\HttpClient\LoggingHttpClient */ | ||
final class LoggingHttpClientTest extends TestCase | ||
{ | ||
public function testWillLogRequestAndResponse(): void | ||
{ | ||
$request = Psr17FactoryDiscovery::findRequestFactory()->createRequest('get', 'http://example.com/foo/bar'); | ||
$response = Psr17FactoryDiscovery::findResponseFactory()->createResponse(204); | ||
|
||
$response->getBody() | ||
->write('hello world'); | ||
|
||
$logger = $this->createMock(LoggerInterface::class); | ||
$next = $this->createMock(ClientInterface::class); | ||
|
||
$next->expects(self::once()) | ||
->method('sendRequest') | ||
->with($request) | ||
->willReturn($response); | ||
|
||
$logger->expects(self::exactly(2)) | ||
->method('debug') | ||
->withConsecutive( | ||
['Sending request {request}', ['request' => $request]], | ||
[ | ||
'Received response {response} to request {request}', | ||
[ | ||
'request' => $request, | ||
'response' => $response, | ||
], | ||
], | ||
); | ||
|
||
self::assertSame( | ||
$response, | ||
(new LoggingHttpClient($next, $logger)) | ||
->sendRequest($request), | ||
); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
test/unit/Monolog/ConvertLogContextHttpRequestsIntoStringsTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Laminas\AutomaticReleases\Test\Unit\Monolog; | ||
|
||
use DateTimeImmutable; | ||
use Http\Discovery\Psr17FactoryDiscovery; | ||
use Laminas\AutomaticReleases\Monolog\ConvertLogContextHttpRequestsIntoStrings; | ||
use Monolog\Level; | ||
use Monolog\LogRecord; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** @covers \Laminas\AutomaticReleases\Monolog\ConvertLogContextHttpRequestsIntoStrings */ | ||
final class ConvertLogContextHttpRequestsIntoStringsTest extends TestCase | ||
{ | ||
public function testWillScrubSensitiveRequestInformation(): void | ||
{ | ||
$date = new DateTimeImmutable(); | ||
|
||
$requestFactory = Psr17FactoryDiscovery::findRequestFactory(); | ||
|
||
$plainRequest = $requestFactory->createRequest('GET', 'http://example.com/foo'); | ||
|
||
$sensitiveRequest = $requestFactory->createRequest('POST', 'https://user:pass@example.com/foo?bar=baz') | ||
->withAddedHeader('Authentication', ['also secret']); | ||
|
||
$sensitiveRequest->getBody() | ||
->write('super: secret'); | ||
|
||
self::assertEquals( | ||
new LogRecord( | ||
$date, | ||
'a-channel', | ||
Level::Critical, | ||
'a message', | ||
[ | ||
'foo' => 'bar', | ||
'plain request' => 'GET http://example.com/foo', | ||
'sensitive request' => 'POST https://example.com/foo?bar=baz', | ||
], | ||
), | ||
(new ConvertLogContextHttpRequestsIntoStrings())(new LogRecord( | ||
$date, | ||
'a-channel', | ||
Level::Critical, | ||
'a message', | ||
[ | ||
'foo' => 'bar', | ||
'plain request' => $plainRequest, | ||
'sensitive request' => $sensitiveRequest, | ||
], | ||
)), | ||
); | ||
} | ||
} |
Oops, something went wrong.