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. */ diff --git a/config/http-logger.php b/config/http-logger.php index 9961d98..4f68600 100644 --- a/config/http-logger.php +++ b/config/http-logger.php @@ -15,10 +15,15 @@ '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. + */ + '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) 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); + } }