From 203e3de7c10ef491acc525721563e585128ab272 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Fri, 16 Feb 2024 09:26:24 +0100 Subject: [PATCH 1/9] Support for trimming the response body --- config/http-client-global-logger.php | 17 ++++++++ src/Listeners/LogResponseReceived.php | 38 ++++++++++++++++- tests/HttpClientLoggerTest.php | 59 +++++++++++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) diff --git a/config/http-client-global-logger.php b/config/http-client-global-logger.php index 2e0de76..f04c190 100644 --- a/config/http-client-global-logger.php +++ b/config/http-client-global-logger.php @@ -107,4 +107,21 @@ 'pass,password,token,apikey,access_token,refresh_token,client_secret' )), ], + + /* + |-------------------------------------------------------------------------- + | Trim response body + |-------------------------------------------------------------------------- + | + | Trim response body to a certain length. This is useful when you are logging + | large responses and you don't want to fill up your log files. + */ + 'trim_response_body' => [ + 'enabled' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_ENABLED', false), + 'treshold' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_TRESHOLD', 200), + 'content_type_whitelist' => explode(',', env( + 'HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_CONTENT_TYPE_WHITELIST', + 'application/json' + )), + ], ]; diff --git a/src/Listeners/LogResponseReceived.php b/src/Listeners/LogResponseReceived.php index 6229a61..0ff1464 100644 --- a/src/Listeners/LogResponseReceived.php +++ b/src/Listeners/LogResponseReceived.php @@ -3,6 +3,8 @@ namespace Onlime\LaravelHttpClientGlobalLogger\Listeners; use GuzzleHttp\MessageFormatter; +use GuzzleHttp\Psr7\Response; +use GuzzleHttp\Psr7\Stream; use Illuminate\Http\Client\Events\ResponseReceived; use Illuminate\Support\Facades\Log; use Onlime\LaravelHttpClientGlobalLogger\EventHelper; @@ -22,7 +24,41 @@ public function handle(ResponseReceived|SentSaloonRequest $event): void $formatter = new MessageFormatter(config('http-client-global-logger.format.response')); Log::channel(config('http-client-global-logger.channel'))->info($formatter->format( EventHelper::getPsrRequest($event), - EventHelper::getPsrResponse($event), + $this->trimBody(EventHelper::getPsrResponse($event)) )); } + + /** + * Trim the response body when it's too long. + */ + private function trimBody(Response $psrResponse): Response + { + // Check if trimming is enabled + if (! config('http-client-global-logger.trim_response_body.enabled')) { + return $psrResponse; + } + + $trimAndLower = fn (string $type) => trim(strtolower($type)); + + $responseContentTypes = array_map($trimAndLower, $psrResponse->getHeader('Content-Type')); + $whiteListedContentTypes = array_map($trimAndLower, config('http-client-global-logger.trim_response_body.content_type_whitelist')); + + // Check if the content type is whitelisted + if (count(array_intersect($responseContentTypes, $whiteListedContentTypes)) > 0) { + return $psrResponse; + } + + $treshold = config('http-client-global-logger.trim_response_body.treshold'); + + // Check if the body size exceeds the treshold + if ($psrResponse->getBody()->getSize() <= $treshold) { + return $psrResponse; + } + + $resource = \fopen('php://memory', 'r+'); + \fwrite($resource, substr($psrResponse->getBody()->__toString(), 0, $treshold).'...'); + \fseek($resource, 0); + + return $psrResponse->withBody(new Stream($resource)); + } } diff --git a/tests/HttpClientLoggerTest.php b/tests/HttpClientLoggerTest.php index 3b10f87..0909403 100644 --- a/tests/HttpClientLoggerTest.php +++ b/tests/HttpClientLoggerTest.php @@ -35,3 +35,62 @@ Http::fake()->get('https://example.com'); }); + +it('can trim the body response', function (array $config, bool $shouldTrim) { + config(['http-client-global-logger.trim_response_body' => $config]); + + HttpClientLogger::addRequestMiddleware(); + + $logger = Mockery::mock(LoggerInterface::class); + + Log::shouldReceive('channel')->with('http-client')->andReturn($logger); + + $logger->shouldReceive('info')->withArgs(function ($message) { + expect($message)->toContain('REQUEST: GET https://example.com'); + return true; + })->once(); + + $logger->shouldReceive('info')->withArgs(function ($message) use ($shouldTrim) { + expect($message)->toContain($shouldTrim ? 'verylongbo...' : 'verylongbody'); + return true; + })->once(); + + Http::fake([ + '*' => Http::response('verylongbody', 200, ['content-type' => 'application/octet-stream']), + ])->get('https://example.com'); +})->with( + [ + 'disabled' => [ + 'config' => [ + 'enabled' => false, + 'treshold' => 10, + 'content_type_whitelist' => ['application/json'], + ], + 'shouldTrim' => false, + ], + 'below_treshold' => [ + 'config' => [ + 'enabled' => true, + 'treshold' => 20, + 'content_type_whitelist' => ['application/json'], + ], + 'shouldTrim' => false, + ], + 'content_type_whitelisted' => [ + 'config' => [ + 'enabled' => true, + 'treshold' => 10, + 'content_type_whitelist' => ['application/octet-stream'], + ], + 'shouldTrim' => false, + ], + 'trim' => [ + 'config' => [ + 'enabled' => true, + 'treshold' => 10, + 'content_type_whitelist' => ['application/json'], + ], + 'shouldTrim' => true, + ], + ] +); From 3ebebc6b8375c46df9436cb72de6c481b1d4e378 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Fri, 16 Feb 2024 09:30:15 +0100 Subject: [PATCH 2/9] Refactor trimming --- src/Listeners/LogResponseReceived.php | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/Listeners/LogResponseReceived.php b/src/Listeners/LogResponseReceived.php index 0ff1464..da734f1 100644 --- a/src/Listeners/LogResponseReceived.php +++ b/src/Listeners/LogResponseReceived.php @@ -4,7 +4,7 @@ use GuzzleHttp\MessageFormatter; use GuzzleHttp\Psr7\Response; -use GuzzleHttp\Psr7\Stream; +use GuzzleHttp\Psr7\Utils; use Illuminate\Http\Client\Events\ResponseReceived; use Illuminate\Support\Facades\Log; use Onlime\LaravelHttpClientGlobalLogger\EventHelper; @@ -55,10 +55,8 @@ private function trimBody(Response $psrResponse): Response return $psrResponse; } - $resource = \fopen('php://memory', 'r+'); - \fwrite($resource, substr($psrResponse->getBody()->__toString(), 0, $treshold).'...'); - \fseek($resource, 0); - - return $psrResponse->withBody(new Stream($resource)); + return $psrResponse->withBody(Utils::streamFor( + substr($psrResponse->getBody(), 0, $treshold).'...' + )); } } From 7cee86e4bef453186cf9b5f80577883dd37bad9a Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Fri, 16 Feb 2024 09:32:58 +0100 Subject: [PATCH 3/9] Refactor test --- tests/HttpClientLoggerTest.php | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tests/HttpClientLoggerTest.php b/tests/HttpClientLoggerTest.php index 0909403..f85bd48 100644 --- a/tests/HttpClientLoggerTest.php +++ b/tests/HttpClientLoggerTest.php @@ -2,21 +2,29 @@ use Illuminate\Support\Facades\Http; use Illuminate\Support\Facades\Log; +use Mockery\MockInterface; use Onlime\LaravelHttpClientGlobalLogger\HttpClientLogger; use Psr\Http\Message\RequestInterface; use Psr\Log\LoggerInterface; -it('can add a global request middleware to log the requests', function () { - Http::globalRequestMiddleware( - fn (RequestInterface $psrRequest) => $psrRequest->withHeader('X-Test', 'test') - ); - +function setupLogger(): MockInterface +{ HttpClientLogger::addRequestMiddleware(); $logger = Mockery::mock(LoggerInterface::class); Log::shouldReceive('channel')->with('http-client')->andReturn($logger); + return $logger; +} + +it('can add a global request middleware to log the requests', function () { + Http::globalRequestMiddleware( + fn (RequestInterface $psrRequest) => $psrRequest->withHeader('X-Test', 'test') + ); + + $logger = setupLogger(); + $logger->shouldReceive('info')->withArgs(function ($message) { expect($message) ->toContain('REQUEST: GET https://example.com') @@ -39,11 +47,7 @@ it('can trim the body response', function (array $config, bool $shouldTrim) { config(['http-client-global-logger.trim_response_body' => $config]); - HttpClientLogger::addRequestMiddleware(); - - $logger = Mockery::mock(LoggerInterface::class); - - Log::shouldReceive('channel')->with('http-client')->andReturn($logger); + $logger = setupLogger(); $logger->shouldReceive('info')->withArgs(function ($message) { expect($message)->toContain('REQUEST: GET https://example.com'); From 11e1de115b416758b0984419767398a93be4643c Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Fri, 16 Feb 2024 09:34:52 +0100 Subject: [PATCH 4/9] Small refactor --- src/Listeners/LogResponseReceived.php | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/Listeners/LogResponseReceived.php b/src/Listeners/LogResponseReceived.php index da734f1..b01dfaf 100644 --- a/src/Listeners/LogResponseReceived.php +++ b/src/Listeners/LogResponseReceived.php @@ -38,8 +38,14 @@ private function trimBody(Response $psrResponse): Response return $psrResponse; } - $trimAndLower = fn (string $type) => trim(strtolower($type)); + $treshold = config('http-client-global-logger.trim_response_body.treshold'); + + // Check if the body size exceeds the treshold + if ($psrResponse->getBody()->getSize() <= $treshold) { + return $psrResponse; + } + $trimAndLower = fn (string $type) => trim(strtolower($type)); $responseContentTypes = array_map($trimAndLower, $psrResponse->getHeader('Content-Type')); $whiteListedContentTypes = array_map($trimAndLower, config('http-client-global-logger.trim_response_body.content_type_whitelist')); @@ -48,13 +54,6 @@ private function trimBody(Response $psrResponse): Response return $psrResponse; } - $treshold = config('http-client-global-logger.trim_response_body.treshold'); - - // Check if the body size exceeds the treshold - if ($psrResponse->getBody()->getSize() <= $treshold) { - return $psrResponse; - } - return $psrResponse->withBody(Utils::streamFor( substr($psrResponse->getBody(), 0, $treshold).'...' )); From 371c54f1c036edf47e143b812932c6d1899368b2 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Fri, 16 Feb 2024 09:39:54 +0100 Subject: [PATCH 5/9] Updated docs --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 57c6ebe..bf05da5 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,9 @@ You may override its configuration in your `.env` - the following environment va - `HTTP_CLIENT_GLOBAL_LOGGER_RESPONSE_FORMAT` (string) - `HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_ENABLED` (bool) - `HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_REPLACEMENT` (string) +- `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_ENABLED` (bool) +- `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_TRESHOLD` (int) +- `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_CONTENT_TYPE_WHITELIST` (string) (look into `config/http-client-global-logger.php` for further configuration and explanation) @@ -45,6 +48,8 @@ Using the logger will log both the request and response of an external HTTP requ - Multi-line log records that contain full request/response information (including all headers and body) - Logging into separate logfile `http-client.log`. You're free to override this and use your own logging channel or just log to a different logfile. - Full support of [Guzzle MessageFormatter](https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php) variable substitutions for highly customized log messages. +- Basic obfuscation of credentials in HTTP Client requests +- Trimming of response body content to a certain length with support for Content-Type whitelisting - **Variant 1: Global logging** (default) - Zero-configuration: Global logging is enabled by default in this package. - Simple and performant implementation using `RequestSending` / `ResponseReceived` event listeners @@ -166,6 +171,7 @@ Both packages provide a different feature set and have those advantages: - auto-configured log channel `http-client` to log to a separate `http-client.log` file - Full support of [Guzzle MessageFormatter](https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php) variable substitutions for highly customized log messages. - basic obfuscation of credentials in HTTP Client requests + - trimming of response body content - [bilfeldt/laravel-http-client-logger](https://github.com/bilfeldt/laravel-http-client-logger) - conditional logging using `logWhen($condition)` - filtering of logs by HTTP response codes From f278af7b300cc4af7dc760c9dded855c53bc9d9a Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Fri, 16 Feb 2024 10:11:05 +0100 Subject: [PATCH 6/9] Use `Str::limit` --- src/Listeners/LogResponseReceived.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Listeners/LogResponseReceived.php b/src/Listeners/LogResponseReceived.php index b01dfaf..6e78117 100644 --- a/src/Listeners/LogResponseReceived.php +++ b/src/Listeners/LogResponseReceived.php @@ -7,6 +7,7 @@ use GuzzleHttp\Psr7\Utils; use Illuminate\Http\Client\Events\ResponseReceived; use Illuminate\Support\Facades\Log; +use Illuminate\Support\Str; use Onlime\LaravelHttpClientGlobalLogger\EventHelper; use Saloon\Laravel\Events\SentSaloonRequest; @@ -55,7 +56,7 @@ private function trimBody(Response $psrResponse): Response } return $psrResponse->withBody(Utils::streamFor( - substr($psrResponse->getBody(), 0, $treshold).'...' + Str::limit($psrResponse->getBody(), $treshold) )); } } From 67de1c15b87d7cb0f74626bb34e71e99d2659128 Mon Sep 17 00:00:00 2001 From: Pascal Baljet Date: Fri, 16 Feb 2024 11:57:59 +0100 Subject: [PATCH 7/9] Better handling of `content-type` header (strip additional values after `;`) --- src/Listeners/LogResponseReceived.php | 16 ++++++++++++---- tests/HttpClientLoggerTest.php | 9 ++++++--- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/src/Listeners/LogResponseReceived.php b/src/Listeners/LogResponseReceived.php index 6e78117..c14b81c 100644 --- a/src/Listeners/LogResponseReceived.php +++ b/src/Listeners/LogResponseReceived.php @@ -46,12 +46,20 @@ private function trimBody(Response $psrResponse): Response return $psrResponse; } - $trimAndLower = fn (string $type) => trim(strtolower($type)); - $responseContentTypes = array_map($trimAndLower, $psrResponse->getHeader('Content-Type')); - $whiteListedContentTypes = array_map($trimAndLower, config('http-client-global-logger.trim_response_body.content_type_whitelist')); + // E.g.: application/json; charset=utf-8 => application/json + $contentTypeHeader = Str::of($psrResponse->getHeaderLine('Content-Type')) + ->before(';') + ->trim() + ->lower() + ->value(); + + $whiteListedContentTypes = array_map( + fn (string $type) => trim(strtolower($type)), + config('http-client-global-logger.trim_response_body.content_type_whitelist') + ); // Check if the content type is whitelisted - if (count(array_intersect($responseContentTypes, $whiteListedContentTypes)) > 0) { + if (in_array($contentTypeHeader, $whiteListedContentTypes)) { return $psrResponse; } diff --git a/tests/HttpClientLoggerTest.php b/tests/HttpClientLoggerTest.php index f85bd48..0ef7cc8 100644 --- a/tests/HttpClientLoggerTest.php +++ b/tests/HttpClientLoggerTest.php @@ -44,7 +44,7 @@ function setupLogger(): MockInterface Http::fake()->get('https://example.com'); }); -it('can trim the body response', function (array $config, bool $shouldTrim) { +it('can trim the body response', function (array $config, bool $shouldTrim, bool $addCharsetToContentType) { config(['http-client-global-logger.trim_response_body' => $config]); $logger = setupLogger(); @@ -60,7 +60,9 @@ function setupLogger(): MockInterface })->once(); Http::fake([ - '*' => Http::response('verylongbody', 200, ['content-type' => 'application/octet-stream']), + '*' => Http::response('verylongbody', 200, [ + 'content-type' => 'application/octet-stream'.($addCharsetToContentType ? '; charset=UTF-8' : ''), + ]), ])->get('https://example.com'); })->with( [ @@ -96,5 +98,6 @@ function setupLogger(): MockInterface ], 'shouldTrim' => true, ], - ] + ], + [true, false] ); From bfb140dff3757a164ee70cacf45d02c18a9c1219 Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Fri, 16 Feb 2024 15:27:48 +0100 Subject: [PATCH 8/9] move threshold check below content-type check, as it might be more resource intensive (getBody()) whitelist empty response Content-Type by default --- config/http-client-global-logger.php | 7 +++++-- src/Listeners/LogResponseReceived.php | 21 ++++++++++----------- tests/HttpClientLoggerTest.php | 26 ++++++++++++++++++++++++-- 3 files changed, 39 insertions(+), 15 deletions(-) diff --git a/config/http-client-global-logger.php b/config/http-client-global-logger.php index f04c190..030b162 100644 --- a/config/http-client-global-logger.php +++ b/config/http-client-global-logger.php @@ -114,14 +114,17 @@ |-------------------------------------------------------------------------- | | Trim response body to a certain length. This is useful when you are logging - | large responses and you don't want to fill up your log files. + | large responses, and you don't want to fill up your log files. + | + | NOTE the leading comma in trim_response_body.content_type_whitelist default value: + | it's there to whitelist empty content types (e.g. when no Content-Type header is set). */ 'trim_response_body' => [ 'enabled' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_ENABLED', false), 'treshold' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_TRESHOLD', 200), 'content_type_whitelist' => explode(',', env( 'HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_CONTENT_TYPE_WHITELIST', - 'application/json' + ',application/json' )), ], ]; diff --git a/src/Listeners/LogResponseReceived.php b/src/Listeners/LogResponseReceived.php index c14b81c..d1c32fa 100644 --- a/src/Listeners/LogResponseReceived.php +++ b/src/Listeners/LogResponseReceived.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Log; use Illuminate\Support\Str; use Onlime\LaravelHttpClientGlobalLogger\EventHelper; +use Psr\Http\Message\MessageInterface; use Saloon\Laravel\Events\SentSaloonRequest; class LogResponseReceived @@ -32,20 +33,13 @@ public function handle(ResponseReceived|SentSaloonRequest $event): void /** * Trim the response body when it's too long. */ - private function trimBody(Response $psrResponse): Response + private function trimBody(Response $psrResponse): Response|MessageInterface { // Check if trimming is enabled if (! config('http-client-global-logger.trim_response_body.enabled')) { return $psrResponse; } - $treshold = config('http-client-global-logger.trim_response_body.treshold'); - - // Check if the body size exceeds the treshold - if ($psrResponse->getBody()->getSize() <= $treshold) { - return $psrResponse; - } - // E.g.: application/json; charset=utf-8 => application/json $contentTypeHeader = Str::of($psrResponse->getHeaderLine('Content-Type')) ->before(';') @@ -63,8 +57,13 @@ private function trimBody(Response $psrResponse): Response return $psrResponse; } - return $psrResponse->withBody(Utils::streamFor( - Str::limit($psrResponse->getBody(), $treshold) - )); + $treshold = config('http-client-global-logger.trim_response_body.treshold'); + + // Check if the body size exceeds the treshold + return ($psrResponse->getBody()->getSize() <= $treshold) + ? $psrResponse + : $psrResponse->withBody(Utils::streamFor( + Str::limit($psrResponse->getBody(), $treshold) + )); } } diff --git a/tests/HttpClientLoggerTest.php b/tests/HttpClientLoggerTest.php index 0ef7cc8..2cb0b6f 100644 --- a/tests/HttpClientLoggerTest.php +++ b/tests/HttpClientLoggerTest.php @@ -44,7 +44,7 @@ function setupLogger(): MockInterface Http::fake()->get('https://example.com'); }); -it('can trim the body response', function (array $config, bool $shouldTrim, bool $addCharsetToContentType) { +it('can trim the body response', function (array $config, string $contentType, bool $shouldTrim, bool $addCharsetToContentType) { config(['http-client-global-logger.trim_response_body' => $config]); $logger = setupLogger(); @@ -61,7 +61,7 @@ function setupLogger(): MockInterface Http::fake([ '*' => Http::response('verylongbody', 200, [ - 'content-type' => 'application/octet-stream'.($addCharsetToContentType ? '; charset=UTF-8' : ''), + 'Content-Type' => $contentType.($addCharsetToContentType ? '; charset=UTF-8' : ''), ]), ])->get('https://example.com'); })->with( @@ -72,6 +72,7 @@ function setupLogger(): MockInterface 'treshold' => 10, 'content_type_whitelist' => ['application/json'], ], + 'contentType' => 'application/octet-stream', 'shouldTrim' => false, ], 'below_treshold' => [ @@ -80,6 +81,7 @@ function setupLogger(): MockInterface 'treshold' => 20, 'content_type_whitelist' => ['application/json'], ], + 'contentType' => 'application/octet-stream', 'shouldTrim' => false, ], 'content_type_whitelisted' => [ @@ -88,6 +90,7 @@ function setupLogger(): MockInterface 'treshold' => 10, 'content_type_whitelist' => ['application/octet-stream'], ], + 'contentType' => 'application/octet-stream', 'shouldTrim' => false, ], 'trim' => [ @@ -96,8 +99,27 @@ function setupLogger(): MockInterface 'treshold' => 10, 'content_type_whitelist' => ['application/json'], ], + 'contentType' => 'application/octet-stream', 'shouldTrim' => true, ], + 'no_content_type_trim' => [ + 'config' => [ + 'enabled' => true, + 'treshold' => 10, + 'content_type_whitelist' => ['application/octet-stream'], + ], + 'contentType' => '', + 'shouldTrim' => true, + ], + 'no_content_type_whitelisted' => [ + 'config' => [ + 'enabled' => true, + 'treshold' => 10, + 'content_type_whitelist' => ['', 'application/octet-stream'], + ], + 'contentType' => '', + 'shouldTrim' => false, + ], ], [true, false] ); From d5fc09cf40dc4c0c6ff5f0f2ac9084c64bee8f46 Mon Sep 17 00:00:00 2001 From: Philip Iezzi Date: Fri, 16 Feb 2024 15:31:54 +0100 Subject: [PATCH 9/9] fix typo in 'threshold', rename to 'limit' --- README.md | 4 ++-- config/http-client-global-logger.php | 2 +- src/Listeners/LogResponseReceived.php | 8 ++++---- tests/HttpClientLoggerTest.php | 14 +++++++------- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index bf05da5..822cd18 100644 --- a/README.md +++ b/README.md @@ -36,10 +36,10 @@ You may override its configuration in your `.env` - the following environment va - `HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_ENABLED` (bool) - `HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_REPLACEMENT` (string) - `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_ENABLED` (bool) -- `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_TRESHOLD` (int) +- `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_LIMIT` (int) - `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_CONTENT_TYPE_WHITELIST` (string) -(look into `config/http-client-global-logger.php` for further configuration and explanation) +(look into `config/http-client-global-logger.php` for defaults, further configuration, and explanation) ## Features diff --git a/config/http-client-global-logger.php b/config/http-client-global-logger.php index 030b162..c9652b2 100644 --- a/config/http-client-global-logger.php +++ b/config/http-client-global-logger.php @@ -121,7 +121,7 @@ */ 'trim_response_body' => [ 'enabled' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_ENABLED', false), - 'treshold' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_TRESHOLD', 200), + 'limit' => env('HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_LIMIT', 200), 'content_type_whitelist' => explode(',', env( 'HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_CONTENT_TYPE_WHITELIST', ',application/json' diff --git a/src/Listeners/LogResponseReceived.php b/src/Listeners/LogResponseReceived.php index d1c32fa..fe4f879 100644 --- a/src/Listeners/LogResponseReceived.php +++ b/src/Listeners/LogResponseReceived.php @@ -57,13 +57,13 @@ private function trimBody(Response $psrResponse): Response|MessageInterface return $psrResponse; } - $treshold = config('http-client-global-logger.trim_response_body.treshold'); + $limit = config('http-client-global-logger.trim_response_body.limit'); - // Check if the body size exceeds the treshold - return ($psrResponse->getBody()->getSize() <= $treshold) + // Check if the body size exceeds the limit + return ($psrResponse->getBody()->getSize() <= $limit) ? $psrResponse : $psrResponse->withBody(Utils::streamFor( - Str::limit($psrResponse->getBody(), $treshold) + Str::limit($psrResponse->getBody(), $limit) )); } } diff --git a/tests/HttpClientLoggerTest.php b/tests/HttpClientLoggerTest.php index 2cb0b6f..c2331a0 100644 --- a/tests/HttpClientLoggerTest.php +++ b/tests/HttpClientLoggerTest.php @@ -69,16 +69,16 @@ function setupLogger(): MockInterface 'disabled' => [ 'config' => [ 'enabled' => false, - 'treshold' => 10, + 'limit' => 10, 'content_type_whitelist' => ['application/json'], ], 'contentType' => 'application/octet-stream', 'shouldTrim' => false, ], - 'below_treshold' => [ + 'below_limit' => [ 'config' => [ 'enabled' => true, - 'treshold' => 20, + 'limit' => 20, 'content_type_whitelist' => ['application/json'], ], 'contentType' => 'application/octet-stream', @@ -87,7 +87,7 @@ function setupLogger(): MockInterface 'content_type_whitelisted' => [ 'config' => [ 'enabled' => true, - 'treshold' => 10, + 'limit' => 10, 'content_type_whitelist' => ['application/octet-stream'], ], 'contentType' => 'application/octet-stream', @@ -96,7 +96,7 @@ function setupLogger(): MockInterface 'trim' => [ 'config' => [ 'enabled' => true, - 'treshold' => 10, + 'limit' => 10, 'content_type_whitelist' => ['application/json'], ], 'contentType' => 'application/octet-stream', @@ -105,7 +105,7 @@ function setupLogger(): MockInterface 'no_content_type_trim' => [ 'config' => [ 'enabled' => true, - 'treshold' => 10, + 'limit' => 10, 'content_type_whitelist' => ['application/octet-stream'], ], 'contentType' => '', @@ -114,7 +114,7 @@ function setupLogger(): MockInterface 'no_content_type_whitelisted' => [ 'config' => [ 'enabled' => true, - 'treshold' => 10, + 'limit' => 10, 'content_type_whitelist' => ['', 'application/octet-stream'], ], 'contentType' => '',