Skip to content

Commit 9f40ccc

Browse files
committed
always handle SendingSaloonRequest, even if the HTTP Client global request middleware was added manually
1 parent ff8a678 commit 9f40ccc

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ So, my recommendation: If you need global logging without any extra configuratio
185185

186186
- No obfuscation of query params, e.g. on a POST request to an OAuth2 token endpoint.
187187

188-
- Obfuscation currently only works in variant 1 or 3 (global logging).
188+
- Obfuscation currently only works in variant 1 or 3 (global logging), and only on requests, not yet on response data.
189189

190190

191191
## Testing

src/EventHelper.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@
1313

1414
class EventHelper
1515
{
16-
public static function shouldBeLogged($event): bool
16+
public static function shouldBeLogged(object $event): bool
1717
{
18+
// Do not log Saloon requests if Saloon is configured to use Laravel's HTTP Client (HttpSender) unless the
19+
// Saloon request is mocked (has a MockClient). Like this, we prevent duplicate logging of Saloon requests,
20+
// as we're also listening to Laravel's HTTP Client events RequestSending and ResponseReceived.
1821
if ($event instanceof SendingSaloonRequest || $event instanceof SentSaloonRequest) {
1922
$saloonUsesHttpSender = config('saloon.default_sender') === HttpSender::class;
20-
2123
return $event->pendingRequest->hasMockClient() || ! $saloonUsesHttpSender;
2224
}
2325

2426
return true;
2527
}
2628

27-
public static function getPsrRequest($event): RequestInterface
29+
public static function getPsrRequest(object $event): RequestInterface
2830
{
2931
return match (true) {
3032
$event instanceof RequestSending || $event instanceof ResponseReceived => $event->request->toPsrRequest(),
@@ -33,7 +35,7 @@ public static function getPsrRequest($event): RequestInterface
3335
};
3436
}
3537

36-
public static function getPsrResponse($event): ResponseInterface
38+
public static function getPsrResponse(object $event): ResponseInterface
3739
{
3840
return match (true) {
3941
$event instanceof ResponseReceived => $event->response->toPsrResponse(),

src/Listeners/LogRequestSending.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,16 @@
1313
class LogRequestSending
1414
{
1515
/**
16-
* Handle the event if the middleware was not added manually.
16+
* Handle the event if the HTTP Client global request middleware was not added manually
17+
* with HttpClientLogger::addRequestMiddleware(). Always handle it for Saloon requests.
1718
*/
1819
public function handle(RequestSending|SendingSaloonRequest $event): void
1920
{
20-
if (! HttpClientLogger::requestMiddlewareWasAdded()) {
21-
$this->handleEvent($event);
21+
if ($event instanceof RequestSending && HttpClientLogger::requestMiddlewareWasAdded()) {
22+
return;
2223
}
24+
25+
$this->handleEvent($event);
2326
}
2427

2528
/**

src/Listeners/LogResponseReceived.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ class LogResponseReceived
1212
{
1313
/**
1414
* Handle the event.
15-
*
16-
* @return void
1715
*/
18-
public function handle(ResponseReceived|SentSaloonRequest $event)
16+
public function handle(ResponseReceived|SentSaloonRequest $event): void
1917
{
2018
if (! EventHelper::shouldBeLogged($event)) {
2119
return;
@@ -24,7 +22,7 @@ public function handle(ResponseReceived|SentSaloonRequest $event)
2422
$formatter = new MessageFormatter(config('http-client-global-logger.format.response'));
2523
Log::channel(config('http-client-global-logger.channel'))->info($formatter->format(
2624
EventHelper::getPsrRequest($event),
27-
EventHelper::getPsrResponse($event)
25+
EventHelper::getPsrResponse($event),
2826
));
2927
}
3028
}

0 commit comments

Comments
 (0)