Skip to content

Commit

Permalink
Merge pull request #50 from Henning256/main
Browse files Browse the repository at this point in the history
customizable log level
  • Loading branch information
freekmurze authored Jun 7, 2022
2 parents 8b94dfd + ea8c2e4 commit 4d1c066
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 5 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
9 changes: 7 additions & 2 deletions config/http-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
4 changes: 2 additions & 2 deletions src/DefaultLogWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions tests/DefaultLogWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4d1c066

Please sign in to comment.