From dfb0736fd15a159d8aefd6958798fa64f6de5f4c Mon Sep 17 00:00:00 2001 From: henning Date: Tue, 7 Jun 2022 09:34:14 +0200 Subject: [PATCH 1/4] customizable log level --- config/http-logger.php | 5 +++++ src/DefaultLogWriter.php | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/config/http-logger.php b/config/http-logger.php index 9961d98..a54e049 100644 --- a/config/http-logger.php +++ b/config/http-logger.php @@ -19,6 +19,11 @@ */ 'log_channel' => env('LOG_CHANNEL', 'stack'), + /* + * The log level used to log the request. + */ + 'log_level' => 'info', + /* * Filter out body fields which will never be logged. */ diff --git a/src/DefaultLogWriter.php b/src/DefaultLogWriter.php index 28d1cae..be00e65 100644 --- a/src/DefaultLogWriter.php +++ b/src/DefaultLogWriter.php @@ -13,7 +13,7 @@ public function logRequest(Request $request) { $message = $this->formatMessage($this->getMessage($request)); - Log::channel(config('http-logger.log_channel'))->info($message); + Log::channel(config('http-logger.log_channel'))->log(config('http-logger.log_level', 'info'), $message); } public function getMessage(Request $request) @@ -37,7 +37,7 @@ protected function formatMessage(array $message) $headersAsJson = json_encode($message['headers']); $files = $message['files']->implode(','); - return "{$message['method']} {$message['uri']} - Body: {$bodyAsJson} - Headers: {$headersAsJson} - Files: ".$files; + return "{$message['method']} {$message['uri']} - Body: {$bodyAsJson} - Headers: {$headersAsJson} - Files: " . $files; } public function flatFiles($file) From fc8fd09c6e3a81085311fc6ee9783feff26d4fb9 Mon Sep 17 00:00:00 2001 From: henning Date: Tue, 7 Jun 2022 10:53:38 +0200 Subject: [PATCH 2/4] fixed comment indention --- config/http-logger.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/config/http-logger.php b/config/http-logger.php index a54e049..4f68600 100644 --- a/config/http-logger.php +++ b/config/http-logger.php @@ -15,13 +15,13 @@ 'log_writer' => \Spatie\HttpLogger\DefaultLogWriter::class, /* - * The log channel used to write the request. - */ + * The log channel used to write the request. + */ 'log_channel' => env('LOG_CHANNEL', 'stack'), /* - * The log level used to log the request. - */ + * The log level used to log the request. + */ 'log_level' => 'info', /* From e45720a6173d53345db7fe4adab8c2e225ead734 Mon Sep 17 00:00:00 2001 From: henning Date: Tue, 7 Jun 2022 10:54:00 +0200 Subject: [PATCH 3/4] added log_level to config example --- README.md | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 26c9bdd..85777a8 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,12 @@ return [ * The log channel used to write the request. */ 'log_channel' => env('LOG_CHANNEL', 'stack'), - + + /* + * The log level used to log the request. + */ + 'log_level' => 'info', + /* * Filter out body fields which will never be logged. */ From ea8c2e46e80f8ee8d09e3ed6463e2bf1c86ff9bd Mon Sep 17 00:00:00 2001 From: henning Date: Tue, 7 Jun 2022 11:06:01 +0200 Subject: [PATCH 4/4] implemented tests for custom log level --- tests/DefaultLogWriterTest.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/DefaultLogWriterTest.php b/tests/DefaultLogWriterTest.php index ad22f9c..3100122 100644 --- a/tests/DefaultLogWriterTest.php +++ b/tests/DefaultLogWriterTest.php @@ -118,4 +118,35 @@ public function it_logs_multiple_files_in_an_array() $this->assertStringContainsString('first.doc', $log); $this->assertStringContainsString('second.doc', $log); } + + /** @test */ + public function it_logs_using_the_default_log_level() + { + $request = $this->makeRequest('post', $this->uri, [ + 'name' => 'Name', + ]); + + $this->logger->logRequest($request); + + $log = $this->readLogFile(); + + $this->assertStringContainsString('testing.INFO', $log); + $this->assertStringContainsString('"name":"Name', $log); + } + + /** @test */ + public function it_logs_using_the_configured_log_level() + { + config(['http-logger.log_level' => 'debug']); + $request = $this->makeRequest('post', $this->uri, [ + 'name' => 'Name', + ]); + + $this->logger->logRequest($request); + + $log = $this->readLogFile(); + + $this->assertStringContainsString('testing.DEBUG', $log); + $this->assertStringContainsString('"name":"Name', $log); + } }