Skip to content

Commit 2d321d4

Browse files
committed
Respect http-client-global-logger.enabled config option also in variant 3 HttpClientLogger::addRequestMiddleware()
Prepare for v1.1.0
1 parent 27b535c commit 2d321d4

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# CHANGELOG
22

3-
## [v1.0.x (Unreleased)](https://github.com/onlime/laravel-http-client-global-logger/compare/v1.0.1...main)
3+
## [v1.1.x (Unreleased)](https://github.com/onlime/laravel-http-client-global-logger/compare/v1.1.0...main)
44

55
- ...
66

7+
## [v1.1.0 (2023-11-09)](https://github.com/onlime/laravel-http-client-global-logger/compare/v1.0.2...v1.1.0)
8+
9+
- Feature | HTTP Request Middleware to log Requests after Global Middleware by @pascalbaljet in #1
10+
711
## [v1.0.2 (2023-07-17)](https://github.com/onlime/laravel-http-client-global-logger/compare/v1.0.1...v1.0.2)
812

913
- Drop Laravel 9 support

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ Using the logger will log both the request and response of an external HTTP requ
5252
- **Variant 2: Mixin** (`HTTP_CLIENT_GLOBAL_LOGGER_MIXIN=true`)
5353
- Enabled only on individual HTTP Client instances, using `Http::log()` - no global logging.
5454
- Log channel name can be set per HTTP Client instance by passing a name to `Http::log($name)`
55+
- **Variant 3: Global HTTP Middleware**
56+
- Can be used in combination with other `Http::globalRequestMiddleware()` calls in your `AppServiceProvider`'s `boot()` method, after registering your [Global Middleware](https://laravel.com/docs/10.x/http-client#global-middleware).
57+
5558

5659
## Usage
5760

61+
> **NOTE:** For all 3 variants below, you need to keep the HTTP Client Global Logger enabled (not setting `HTTP_CLIENT_GLOBAL_LOGGER_ENABLED=false` in your `.env`). The `http-client-global-logger.enabled` config option is a global on/off switch for all 3 variants, not just the "global" variants. Our project name might be misleading in that context.
62+
5863
### Variant 1: Global Logging
5964

6065
**Just use Laravel HTTP Client as always - no need to configure anything!**
@@ -105,7 +110,7 @@ $response = $client->get('/user');
105110

106111
### Variant 3: Global HTTP Middleware
107112

108-
If you use [Global Middleware](https://laravel.com/docs/10.x/http-client#global-middleware), you should be aware that *Variant 1* uses Laravel's `RequestSending` event to log HTTP requests. This event is fired **before** Global Middleware is executed. Therefore, any modifications to the request made by Global Middleware will not be logged. To overcome this, this package provides a middleware that you may add after your Global Middleware.
113+
If you use [Global Middleware](https://laravel.com/docs/10.x/http-client#global-middleware) (`Http::globalRequestMiddleware()` and `Http::globalResponseMiddleware()` methods), you should be aware that *Variant 1* uses Laravel's `RequestSending` event to log HTTP requests. This event is fired **before** Global Middleware is executed. Therefore, any modifications to the request made by Global Middleware will not be logged. To overcome this, this package provides a middleware that you may add after your Global Middleware.
109114

110115
You may add the middleware using the static `addRequestMiddleware()` method on the `HttpClientLogger` class:
111116

src/HttpClientLogger.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ class HttpClientLogger
1818

1919
public static function addRequestMiddleware(): void
2020
{
21+
if (! config('http-client-global-logger.enabled')) {
22+
return;
23+
}
24+
2125
Http::globalRequestMiddleware(
2226
fn (RequestInterface $psrRequest) => tap($psrRequest, function (RequestInterface $psrRequest) {
2327
// Wrap PSR-7 request into Laravel's HTTP Client Request object

src/Listeners/LogRequestSending.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,8 @@ class LogRequestSending
1212
{
1313
/**
1414
* Handle the event if the middleware was not added manually.
15-
*
16-
* @return void
1715
*/
18-
public function handle(RequestSending $event)
16+
public function handle(RequestSending $event): void
1917
{
2018
if (!HttpClientLogger::requestMiddlewareWasAdded()) {
2119
$this->handleEvent($event);
@@ -24,10 +22,8 @@ public function handle(RequestSending $event)
2422

2523
/**
2624
* Handle the event.
27-
*
28-
* @return void
2925
*/
30-
public function handleEvent(RequestSending $event)
26+
public function handleEvent(RequestSending $event): void
3127
{
3228
$obfuscate = config('http-client-global-logger.obfuscate.enabled');
3329
$psrRequest = $event->request->toPsrRequest();

0 commit comments

Comments
 (0)