Skip to content

Commit 8f8e6f1

Browse files
authored
Merge pull request #8 from bilfeldt/fix/refactor-config
Refactor configuration
2 parents 36f432f + 1c6b0f1 commit 8f8e6f1

File tree

6 files changed

+71
-50
lines changed

6 files changed

+71
-50
lines changed

CHANGELOG.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,34 @@ All notable changes to `laravel-http-client-logger` will be documented in this f
44

55
## Upgrade guides
66

7+
### 0.3.0 => 1.0.0
8+
9+
This release flattens the configuration variables. It is suggested to republish the configuration after upgrading.
10+
11+
- `filtering.always` is renamed to `filter_all`
12+
- `filtering.2xx` is renamed to `filter_2xx`
13+
- `filtering.3xx` is renamed to `filter_3xx`
14+
- `filtering.4xx` is renamed to `filter_4xx`
15+
- `filtering.5xx` is renamed to `filter_5xx`
16+
- `filtering.slow` is renamed to `filter_slow`
17+
- `log_to_channel.enabled` has been removed, instead logging to channel is enabled when a channel is provided
18+
- `log_to_channel.channel` is renamed to `channel`
19+
- `log_to_disk.enabled` has been removed, instead logging to disk is enabled when a disk is provided
20+
- `log_to_disk.disk` is renamed to `disk`
21+
- `log_to_disk.separate` is renamed to `disk_separate_files`
22+
- `log_to_disk.timestamp` is renamed to `prefix_timestamp`
23+
- `log_to_disk.filename` is renamed to `filename`
24+
25+
The following environment variables have been renamed:
26+
- `HTTP_CLIENT_LOGGER_FILTERING_ALWAYS` is renamed to `HTTP_CLIENT_LOGGER_FILTER_ALL`
27+
- `HTTP_CLIENT_LOGGER_FILTERING_2XX` is renamed to `HTTP_CLIENT_LOGGER_FILTER_2XX`
28+
- `HTTP_CLIENT_LOGGER_FILTERING_3XX` is renamed to `HTTP_CLIENT_LOGGER_FILTER_3XX`
29+
- `HTTP_CLIENT_LOGGER_FILTERING_4XX` is renamed to `HTTP_CLIENT_LOGGER_FILTER_4XX`
30+
- `HTTP_CLIENT_LOGGER_FILTERING_5XX` is renamed to `HTTP_CLIENT_LOGGER_FILTER_5XX`
31+
- `HTTP_CLIENT_LOGGER_FILTERING_SLOW` is renamed to `HTTP_CLIENT_LOGGER_FILTER_SLOW`
32+
- `HTTP_CLIENT_LOGGER_CHANNEL_LOG_ENABLED` removed in favor of `HTTP_CLIENT_LOGGER_CHANNEL`
33+
- `HTTP_CLIENT_LOGGER_DISK_LOG_ENABLED` removed in favor of `HTTP_CLIENT_LOGGER_DISK`
34+
735
### 0.2.0 => 0.3.0
836

937
This release includes breaking changes:

config/http-client-logger.php

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,17 @@
3434
| that these settings are only used by the default filter.
3535
|
3636
*/
37-
'filtering' => [
38-
'always' => env('HTTP_CLIENT_LOGGER_FILTERING_ALWAYS', false),
37+
'filter_always' => env('HTTP_CLIENT_LOGGER_FILTER_ALL', false),
3938

40-
'2xx' => env('HTTP_CLIENT_LOGGER_FILTERING_2XX', true),
39+
'filter_2xx' => env('HTTP_CLIENT_LOGGER_FILTER_2XX', true),
4140

42-
'3xx' => env('HTTP_CLIENT_LOGGER_FILTERING_3XX', true),
41+
'filter_3xx' => env('HTTP_CLIENT_LOGGER_FILTER_3XX', true),
4342

44-
'4xx' => env('HTTP_CLIENT_LOGGER_FILTERING_4XX', true),
43+
'filter_4xx' => env('HTTP_CLIENT_LOGGER_FILTER_4XX', true),
4544

46-
'5xx' => env('HTTP_CLIENT_LOGGER_FILTERING_5XX', true),
45+
'filter_5xx' => env('HTTP_CLIENT_LOGGER_FILTER_5XX', true),
4746

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

5149
/*
5250
|--------------------------------------------------------------------------
@@ -62,32 +60,27 @@
6260

6361
/*
6462
|--------------------------------------------------------------------------
65-
| Logger to channel
63+
| Log to channel
6664
|--------------------------------------------------------------------------
6765
|
6866
| These settings determine how to log request/responses to the Laravel log.
6967
| Note that these settings are only used by the default logger.
68+
| Set to false to disable the channel logging.
7069
|
7170
*/
72-
'log_to_channel' => [
73-
'enabled' => env('HTTP_CLIENT_LOGGER_CHANNEL_LOG_ENABLED', true),
74-
'channel' => env('HTTP_CLIENT_LOGGER_CHANNEL'), // Uses the default log channel unless specified
75-
],
71+
'channel' => env('HTTP_CLIENT_LOGGER_CHANNEL', 'default'),
7672

7773
/*
7874
|--------------------------------------------------------------------------
79-
| Logger to disk
75+
| Log to disk
8076
|--------------------------------------------------------------------------
8177
|
8278
| These settings determine how to log request/responses to a flysystem disk.
8379
| Note that these settings are only used by the default logger.
8480
|
8581
*/
86-
'log_to_disk' => [
87-
'enabled' => env('HTTP_CLIENT_LOGGER_DISK_LOG_ENABLED', true),
88-
'disk' => env('HTTP_CLIENT_LOGGER_DISK'), // uses the default filesystem disk if none is specified
89-
'separate' => true,
90-
'timestamp' => 'Y-m-d-Hisu', // Leaving empty will remove the timestamp
91-
'filename' => '',
92-
],
82+
'disk' => env('HTTP_CLIENT_LOGGER_DISK', false),
83+
'disk_separate_files' => true,
84+
'prefix_timestamp' => 'Y-m-d-Hisu', // Leaving empty will remove the timestamp
85+
'filename' => '',
9386
];

src/HttpLogger.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,14 @@ public function log(
3636
$this->response = $response;
3737
$this->sec = $sec;
3838
$this->context = $context;
39-
$this->config = array_replace_recursive(config('http-client-logger'), $config); // Note this does not work optimally!
39+
$this->config = array_merge(config('http-client-logger'), $config);
4040

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

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

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

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

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

8383
protected function logToDisk(string $disk): void
8484
{
85-
if (Arr::get($this->config, 'log_to_disk.separate')) {
85+
if (Arr::get($this->config, 'disk_separate_files')) {
8686
Storage::disk($disk)->put(
8787
$this->getFileName().'-request'.Str::start($this->fileExt, '.'),
8888
$this->psrMessageStringConverter->toString($this->request, $this->getReplace())

src/HttpLoggingFilter.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,27 @@ public function shouldLog(
1818
return false;
1919
}
2020

21-
if (config('http-client-logger.filtering.always')) {
21+
if (config('http-client-logger.filter_always')) {
2222
return true;
2323
}
2424

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

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

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

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

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

tests/HttpLoggingFilterTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ public function setUp(): void
1919

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

2424
$this->assertTrue($this->filter->shouldLog(
2525
new Request('GET', '/'),
2626
new Response(200),
2727
0
2828
));
2929

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

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

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

4343
$this->assertTrue($this->filter->shouldLog(
4444
new Request('GET', '/'),
4545
new Response(300),
4646
0
4747
));
4848

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

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

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

6262
$this->assertTrue($this->filter->shouldLog(
6363
new Request('GET', '/'),
6464
new Response(400),
6565
0
6666
));
6767

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

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

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

8181
$this->assertTrue($this->filter->shouldLog(
8282
new Request('GET', '/'),
8383
new Response(500),
8484
0
8585
));
8686

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

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

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

101101
$this->assertFalse($this->filter->shouldLog(
102102
new Request('GET', '/'),

tests/TestCase.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ public function setUp(): void
1313

1414
parent::setUp();
1515

16-
config()->set('http-client-logger.filtering.2xx', true);
17-
config()->set('http-client-logger.filtering.3xx', true);
18-
config()->set('http-client-logger.filtering.4xx', true);
19-
config()->set('http-client-logger.filtering.5xx', true);
20-
config()->set('http-client-logger.filtering.slow', true);
16+
config()->set('http-client-logger.filter_2xx', true);
17+
config()->set('http-client-logger.filter_3xx', true);
18+
config()->set('http-client-logger.filter_4xx', true);
19+
config()->set('http-client-logger.filter_5xx', true);
20+
config()->set('http-client-logger.filter_slow', true);
2121
}
2222

2323
protected function getPackageProviders($app)

0 commit comments

Comments
 (0)