Skip to content

Refactor configuration #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,34 @@ All notable changes to `laravel-http-client-logger` will be documented in this f

## Upgrade guides

### 0.3.0 => 1.0.0

This release flattens the configuration variables. It is suggested to republish the configuration after upgrading.

- `filtering.always` is renamed to `filter_all`
- `filtering.2xx` is renamed to `filter_2xx`
- `filtering.3xx` is renamed to `filter_3xx`
- `filtering.4xx` is renamed to `filter_4xx`
- `filtering.5xx` is renamed to `filter_5xx`
- `filtering.slow` is renamed to `filter_slow`
- `log_to_channel.enabled` has been removed, instead logging to channel is enabled when a channel is provided
- `log_to_channel.channel` is renamed to `channel`
- `log_to_disk.enabled` has been removed, instead logging to disk is enabled when a disk is provided
- `log_to_disk.disk` is renamed to `disk`
- `log_to_disk.separate` is renamed to `disk_separate_files`
- `log_to_disk.timestamp` is renamed to `prefix_timestamp`
- `log_to_disk.filename` is renamed to `filename`

The following environment variables have been renamed:
- `HTTP_CLIENT_LOGGER_FILTERING_ALWAYS` is renamed to `HTTP_CLIENT_LOGGER_FILTER_ALL`
- `HTTP_CLIENT_LOGGER_FILTERING_2XX` is renamed to `HTTP_CLIENT_LOGGER_FILTER_2XX`
- `HTTP_CLIENT_LOGGER_FILTERING_3XX` is renamed to `HTTP_CLIENT_LOGGER_FILTER_3XX`
- `HTTP_CLIENT_LOGGER_FILTERING_4XX` is renamed to `HTTP_CLIENT_LOGGER_FILTER_4XX`
- `HTTP_CLIENT_LOGGER_FILTERING_5XX` is renamed to `HTTP_CLIENT_LOGGER_FILTER_5XX`
- `HTTP_CLIENT_LOGGER_FILTERING_SLOW` is renamed to `HTTP_CLIENT_LOGGER_FILTER_SLOW`
- `HTTP_CLIENT_LOGGER_CHANNEL_LOG_ENABLED` removed in favor of `HTTP_CLIENT_LOGGER_CHANNEL`
- `HTTP_CLIENT_LOGGER_DISK_LOG_ENABLED` removed in favor of `HTTP_CLIENT_LOGGER_DISK`

### 0.2.0 => 0.3.0

This release includes breaking changes:
Expand Down
35 changes: 14 additions & 21 deletions config/http-client-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,17 @@
| that these settings are only used by the default filter.
|
*/
'filtering' => [
'always' => env('HTTP_CLIENT_LOGGER_FILTERING_ALWAYS', false),
'filter_always' => env('HTTP_CLIENT_LOGGER_FILTER_ALL', false),

'2xx' => env('HTTP_CLIENT_LOGGER_FILTERING_2XX', true),
'filter_2xx' => env('HTTP_CLIENT_LOGGER_FILTER_2XX', true),

'3xx' => env('HTTP_CLIENT_LOGGER_FILTERING_3XX', true),
'filter_3xx' => env('HTTP_CLIENT_LOGGER_FILTER_3XX', true),

'4xx' => env('HTTP_CLIENT_LOGGER_FILTERING_4XX', true),
'filter_4xx' => env('HTTP_CLIENT_LOGGER_FILTER_4XX', true),

'5xx' => env('HTTP_CLIENT_LOGGER_FILTERING_5XX', true),
'filter_5xx' => env('HTTP_CLIENT_LOGGER_FILTER_5XX', true),

'slow' => env('HTTP_CLIENT_LOGGER_FILTERING_SLOW', 1.5), // Log requests that took longer than the setting (in sec)
],
'filter_slow' => env('HTTP_CLIENT_LOGGER_FILTER_SLOW', 1.5), // Log requests that took longer than the setting (in sec)

/*
|--------------------------------------------------------------------------
Expand All @@ -62,32 +60,27 @@

/*
|--------------------------------------------------------------------------
| Logger to channel
| Log to channel
|--------------------------------------------------------------------------
|
| These settings determine how to log request/responses to the Laravel log.
| Note that these settings are only used by the default logger.
| Set to false to disable the channel logging.
|
*/
'log_to_channel' => [
'enabled' => env('HTTP_CLIENT_LOGGER_CHANNEL_LOG_ENABLED', true),
'channel' => env('HTTP_CLIENT_LOGGER_CHANNEL'), // Uses the default log channel unless specified
],
'channel' => env('HTTP_CLIENT_LOGGER_CHANNEL', 'default'),

/*
|--------------------------------------------------------------------------
| Logger to disk
| Log to disk
|--------------------------------------------------------------------------
|
| These settings determine how to log request/responses to a flysystem disk.
| Note that these settings are only used by the default logger.
|
*/
'log_to_disk' => [
'enabled' => env('HTTP_CLIENT_LOGGER_DISK_LOG_ENABLED', true),
'disk' => env('HTTP_CLIENT_LOGGER_DISK'), // uses the default filesystem disk if none is specified
'separate' => true,
'timestamp' => 'Y-m-d-Hisu', // Leaving empty will remove the timestamp
'filename' => '',
],
'disk' => env('HTTP_CLIENT_LOGGER_DISK', false),
'disk_separate_files' => true,
'prefix_timestamp' => 'Y-m-d-Hisu', // Leaving empty will remove the timestamp
'filename' => '',
];
16 changes: 8 additions & 8 deletions src/HttpLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public function log(
$this->response = $response;
$this->sec = $sec;
$this->context = $context;
$this->config = array_replace_recursive(config('http-client-logger'), $config); // Note this does not work optimally!
$this->config = array_merge(config('http-client-logger'), $config);

if (Arr::get($this->config, 'log_to_channel.enabled')) {
$this->logToChannel(Arr::get($this->config, 'log_to_channel.channel') ?? config('logging.default'));
if (Arr::get($this->config, 'channel')) {
$this->logToChannel(($channel = Arr::get($this->config, 'channel')) == 'default' ? config('logging.default') : $channel);
}

if (Arr::get($this->config, 'log_to_disk.enabled')) {
$this->logToDisk(Arr::get($this->config, 'log_to_disk.disk') ?? config('filesystems.default'));
if (Arr::get($this->config, 'disk')) {
$this->logToDisk(($disk = Arr::get($this->config, 'disk')) == 'default' ? config('filesystems.default') : $disk);
}
}

Expand All @@ -54,8 +54,8 @@ protected function getReplace(): array

protected function getFileName(): string
{
return (Arr::get($this->config, 'log_to_disk.timestamp') ? now()->format(Arr::get($this->config, 'log_to_disk.timestamp')) : '')
.Arr::get($this->config, 'log_to_disk.filename');
return (Arr::get($this->config, 'prefix_timestamp') ? now()->format(Arr::get($this->config, 'prefix_timestamp')) : '')
.Arr::get($this->config, 'filename');
}

protected function getMessage(): string
Expand All @@ -82,7 +82,7 @@ protected function logToChannel(string $channel): void

protected function logToDisk(string $disk): void
{
if (Arr::get($this->config, 'log_to_disk.separate')) {
if (Arr::get($this->config, 'disk_separate_files')) {
Storage::disk($disk)->put(
$this->getFileName().'-request'.Str::start($this->fileExt, '.'),
$this->psrMessageStringConverter->toString($this->request, $this->getReplace())
Expand Down
12 changes: 6 additions & 6 deletions src/HttpLoggingFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,27 @@ public function shouldLog(
return false;
}

if (config('http-client-logger.filtering.always')) {
if (config('http-client-logger.filter_always')) {
return true;
}

if (config('http-client-logger.filtering.2xx') && $response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
if (config('http-client-logger.filter_2xx') && $response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
return true;
}

if (config('http-client-logger.filtering.3xx') && $response->getStatusCode() >= 300 && $response->getStatusCode() < 400) {
if (config('http-client-logger.filter_3xx') && $response->getStatusCode() >= 300 && $response->getStatusCode() < 400) {
return true;
}

if (config('http-client-logger.filtering.4xx') && $response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
if (config('http-client-logger.filter_4xx') && $response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
return true;
}

if (config('http-client-logger.filtering.5xx') && $response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
if (config('http-client-logger.filter_5xx') && $response->getStatusCode() >= 500 && $response->getStatusCode() < 600) {
return true;
}

if (config('http-client-logger.filtering.slow') < $sec) {
if (config('http-client-logger.filter_slow') < $sec) {
return true;
}

Expand Down
20 changes: 10 additions & 10 deletions tests/HttpLoggingFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ public function setUp(): void

public function test_filter_log_2xx()
{
config(['http-client-logger.filtering.2xx' => true]);
config(['http-client-logger.filter_2xx' => true]);

$this->assertTrue($this->filter->shouldLog(
new Request('GET', '/'),
new Response(200),
0
));

config(['http-client-logger.filtering.2xx' => false]);
config(['http-client-logger.filter_2xx' => false]);

$this->assertFalse($this->filter->shouldLog(
new Request('GET', '/'),
Expand All @@ -38,15 +38,15 @@ public function test_filter_log_2xx()

public function test_filter_log_3xx()
{
config(['http-client-logger.filtering.3xx' => true]);
config(['http-client-logger.filter_3xx' => true]);

$this->assertTrue($this->filter->shouldLog(
new Request('GET', '/'),
new Response(300),
0
));

config(['http-client-logger.filtering.3xx' => false]);
config(['http-client-logger.filter_3xx' => false]);

$this->assertFalse($this->filter->shouldLog(
new Request('GET', '/'),
Expand All @@ -57,15 +57,15 @@ public function test_filter_log_3xx()

public function test_filter_log_4xx()
{
config(['http-client-logger.filtering.4xx' => true]);
config(['http-client-logger.filter_4xx' => true]);

$this->assertTrue($this->filter->shouldLog(
new Request('GET', '/'),
new Response(400),
0
));

config(['http-client-logger.filtering.4xx' => false]);
config(['http-client-logger.filter_4xx' => false]);

$this->assertFalse($this->filter->shouldLog(
new Request('GET', '/'),
Expand All @@ -76,15 +76,15 @@ public function test_filter_log_4xx()

public function test_filter_log_5xx()
{
config(['http-client-logger.filtering.5xx' => true]);
config(['http-client-logger.filter_5xx' => true]);

$this->assertTrue($this->filter->shouldLog(
new Request('GET', '/'),
new Response(500),
0
));

config(['http-client-logger.filtering.5xx' => false]);
config(['http-client-logger.filter_5xx' => false]);

$this->assertFalse($this->filter->shouldLog(
new Request('GET', '/'),
Expand All @@ -95,8 +95,8 @@ public function test_filter_log_5xx()

public function test_filter_log_slow()
{
config(['http-client-logger.filtering.2xx' => false]);
config(['http-client-logger.filtering.slow' => 1.5]);
config(['http-client-logger.filter_2xx' => false]);
config(['http-client-logger.filter_slow' => 1.5]);

$this->assertFalse($this->filter->shouldLog(
new Request('GET', '/'),
Expand Down
10 changes: 5 additions & 5 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public function setUp(): void

parent::setUp();

config()->set('http-client-logger.filtering.2xx', true);
config()->set('http-client-logger.filtering.3xx', true);
config()->set('http-client-logger.filtering.4xx', true);
config()->set('http-client-logger.filtering.5xx', true);
config()->set('http-client-logger.filtering.slow', true);
config()->set('http-client-logger.filter_2xx', true);
config()->set('http-client-logger.filter_3xx', true);
config()->set('http-client-logger.filter_4xx', true);
config()->set('http-client-logger.filter_5xx', true);
config()->set('http-client-logger.filter_slow', true);
}

protected function getPackageProviders($app)
Expand Down