Skip to content

Commit cf0100b

Browse files
committed
Update configuration and add disk storage
1 parent 09ef22c commit cf0100b

File tree

6 files changed

+197
-47
lines changed

6 files changed

+197
-47
lines changed

config/http-client-logger.php

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,89 @@
11
<?php
22

33
return [
4-
'logger' => \Bilfeldt\LaravelHttpClientLogger\HttpLogger::class,
4+
/*
5+
|--------------------------------------------------------------------------
6+
| Filter class
7+
|--------------------------------------------------------------------------
8+
|
9+
| This is the filter used (unless specified at runtime) to determine if a
10+
| request should be logged or not. Any custom filter can be inserted here
11+
| but remember that it must implement the HttpLoggingFilterInterface.
12+
|
13+
*/
14+
'filter' => \Bilfeldt\LaravelHttpClientLogger\HttpLoggingFilter::class,
515

6-
'log_channel' => null,
16+
/*
17+
|--------------------------------------------------------------------------
18+
| Enable logging
19+
|--------------------------------------------------------------------------
20+
|
21+
| Whether or not logging should be enabled/disabled when using the filter
22+
| specified above. Specifying another filter at runtime will override this
23+
| setting.
24+
|
25+
*/
26+
'enabled' => env('HTTP_CLIENT_LOGGER_ENABLED', true),
727

8-
'filter' => \Bilfeldt\LaravelHttpClientLogger\HttpLoggingFilter::class,
28+
/*
29+
|--------------------------------------------------------------------------
30+
| Filtering options
31+
|--------------------------------------------------------------------------
32+
|
33+
| These settings determine what request/responses should be logged. Note
34+
| that these settings are only used by the default filter.
35+
|
36+
*/
37+
'filtering' => [
38+
'2xx' => true,
939

10-
'filter_2xx' => true,
40+
'3xx' => true,
1141

12-
'filter_3xx' => true,
42+
'4xx' => true,
1343

14-
'filter_4xx' => true,
44+
'5xx' => true,
45+
46+
'slow' => 1.5, // Log requests that took longer than the setting (in sec)
47+
],
48+
49+
/*
50+
|--------------------------------------------------------------------------
51+
| Logger class
52+
|--------------------------------------------------------------------------
53+
|
54+
| This is the logger used (unless specified at runtime) to actually log
55+
| the request and response. Any custom logger can be inserted here
56+
| but remember that it must implement the HttpLoggerInterface.
57+
|
58+
*/
59+
'logger' => \Bilfeldt\LaravelHttpClientLogger\HttpLogger::class,
1560

16-
'filter_5xx' => true,
61+
/*
62+
|--------------------------------------------------------------------------
63+
| Logger to channel
64+
|--------------------------------------------------------------------------
65+
|
66+
| These settings determine how to log request/responses to the Laravel log.
67+
| Note that these settings are only used by the default logger.
68+
|
69+
*/
70+
'log_to_channel' => [
71+
'enabled' => env('HTTP_CLIENT_LOGGER_CHANNEL_LOG_ENABLED', true),
72+
'channel' => env('HTTP_CLIENT_LOGGER_CHANNEL'), // Uses the default log channel unless specified
73+
],
1774

18-
'filter_slow' => 1.5,
75+
/*
76+
|--------------------------------------------------------------------------
77+
| Logger to disk
78+
|--------------------------------------------------------------------------
79+
|
80+
| These settings determine how to log request/responses to a flysystem disk.
81+
| Note that these settings are only used by the default logger.
82+
|
83+
*/
84+
'log_to_disk' => [
85+
'enabled' => env('HTTP_CLIENT_LOGGER_DISK_LOG_ENABLED', true),
86+
'disk' => env('HTTP_CLIENT_LOGGER_DISK'), // uses the default filesystem disk unless specified
87+
'separate' => true,
88+
],
1989
];

src/HttpLogger.php

Lines changed: 81 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,106 @@
22

33
namespace Bilfeldt\LaravelHttpClientLogger;
44

5-
use GuzzleHttp\Psr7\Message;
65
use Illuminate\Support\Arr;
76
use Illuminate\Support\Facades\Log;
8-
use Psr\Http\Message\MessageInterface;
7+
use Illuminate\Support\Facades\Storage;
8+
use Illuminate\Support\Str;
99
use Psr\Http\Message\RequestInterface;
1010
use Psr\Http\Message\ResponseInterface;
1111

1212
class HttpLogger implements HttpLoggerInterface
1313
{
14+
protected PsrMessageToStringConverter $psrMessageStringConverter;
15+
16+
protected RequestInterface $request;
17+
protected ResponseInterface $response;
18+
protected float $sec;
19+
protected array $context;
20+
protected array $replace;
21+
protected string $filename;
22+
protected string $fileExt = '.txt';
23+
24+
public function __construct(PsrMessageToStringConverter $psrMessageStringConverter)
25+
{
26+
$this->psrMessageStringConverter = $psrMessageStringConverter;
27+
}
28+
1429
public function log(
1530
RequestInterface $request,
1631
ResponseInterface $response,
1732
float $sec,
1833
array $context = []
19-
): void {
20-
$message = "Time {$sec}sec\r\n"
21-
."Request\r\n"
22-
.$this->messageToString($request, Arr::get($context, 'replace', []))."\r\n"
23-
."Response\r\n"
24-
.$this->messageToString($response, Arr::get($context, 'replace', []));
34+
): void
35+
{
36+
$this->request = $request;
37+
$this->response = $response;
38+
$this->sec = $sec;
39+
$this->replace = $this->getReplace($context);
40+
$this->filename = $this->getFileName($context);
41+
$this->context = $this->getContextCleaned($context); // must be called after the two above since the $context array is modified
42+
43+
if (config('http-client-logger.log_to_channel.enabled')) {
44+
$this->logToChannel(config('http-client-logger.log_to_channel.channel') ?? config('logging.default'));
45+
}
46+
47+
if (config('http-client-logger.log_to_disk.enabled')) {
48+
$this->logToDisk(config('http-client-logger.log_to_disk.disk') ?? config('filesystems.default'));
49+
}
50+
}
51+
52+
protected function getContextCleaned(array $context): array
53+
{
54+
return Arr::except($context, ['replace', 'filename']);
55+
}
2556

26-
$context = Arr::except($context, 'replace');
57+
protected function getReplace(array $context): array
58+
{
59+
return Arr::get($context, 'replace', []);
60+
}
61+
62+
protected function getFileName(array $context): string
63+
{
64+
return Arr::get($context, 'filename', now()->format('Y-m-d-Hisu'));
65+
}
2766

28-
$channel = config('http-client-logger.log_channel') ?? config('logging.default');
67+
protected function getMessage(): string
68+
{
69+
return "Time {$this->sec}sec\r\n"
70+
."Request\r\n"
71+
.$this->psrMessageStringConverter->toString($this->request, $this->replace)."\r\n"
72+
."Response\r\n"
73+
.$this->psrMessageStringConverter->toString($this->response, $this->replace);
74+
}
2975

30-
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
31-
Log::channel($channel)->debug($message, $context);
32-
} elseif ($response->getStatusCode() >= 300 && $response->getStatusCode() < 400) {
33-
Log::channel($channel)->info($message, $context);
34-
} elseif ($response->getStatusCode() >= 400 && $response->getStatusCode() < 500) {
35-
Log::channel($channel)->error($message, $context);
76+
protected function logToChannel(string $channel): void
77+
{
78+
if ($this->response->getStatusCode() >= 200 && $this->response->getStatusCode() < 300) {
79+
Log::channel($channel)->debug($this->getMessage(), $this->context);
80+
} elseif ($this->response->getStatusCode() >= 300 && $this->response->getStatusCode() < 400) {
81+
Log::channel($channel)->info($this->getMessage(), $this->context);
82+
} elseif ($this->response->getStatusCode() >= 400 && $this->response->getStatusCode() < 500) {
83+
Log::channel($channel)->error($this->getMessage(), $this->context);
3684
} else {
37-
Log::channel($channel)->critical($message, $context);
85+
Log::channel($channel)->critical($this->getMessage(), $this->context);
3886
}
3987
}
4088

41-
protected function messageToString(MessageInterface $message, array $placeholders): string
89+
protected function logToDisk(string $disk): void
4290
{
43-
return strtr(Message::toString($message), $placeholders);
91+
if (config('http-client-logger.log_to_disk.separate')) {
92+
Storage::disk($disk)->put(
93+
$this->filename.'-request'.Str::start($this->fileExt, '.'),
94+
$this->psrMessageStringConverter->toString($this->request, $this->replace)
95+
);
96+
Storage::disk($disk)->put(
97+
$this->filename.'-response'.Str::start($this->fileExt, '.'),
98+
$this->psrMessageStringConverter->toString($this->response, $this->replace)
99+
);
100+
} else {
101+
Storage::disk($disk)->put(
102+
$this->filename.Str::start($this->fileExt, '.'),
103+
$this->getMessage()
104+
);
105+
}
44106
}
45107
}

src/HttpLoggingFilter.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,27 @@ public function shouldLog(
1313
float $sec,
1414
array $context = []
1515
): bool {
16-
if (config('http-client-logger.filter_2xx') && $response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
16+
if (! config('http-client-logger.enabled')) {
17+
return false;
18+
}
19+
20+
if (config('http-client-logger.filtering.2xx') && $response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
1721
return true;
1822
}
1923

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

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

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

32-
if (config('http-client-logger.filter_slow') < $sec) {
36+
if (config('http-client-logger.filtering.slow') < $sec) {
3337
return true;
3438
}
3539

src/PsrMessageToStringConverter.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Bilfeldt\LaravelHttpClientLogger;
4+
5+
use GuzzleHttp\Psr7\Message;
6+
use Psr\Http\Message\MessageInterface;
7+
8+
class PsrMessageToStringConverter
9+
{
10+
public function toString(MessageInterface $message, array $placeholders): string
11+
{
12+
return strtr(Message::toString($message), $placeholders);
13+
}
14+
}

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.filter_2xx' => true]);
22+
config(['http-client-logger.filtering.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.filter_2xx' => false]);
30+
config(['http-client-logger.filtering.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.filter_3xx' => true]);
41+
config(['http-client-logger.filtering.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.filter_3xx' => false]);
49+
config(['http-client-logger.filtering.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.filter_4xx' => true]);
60+
config(['http-client-logger.filtering.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.filter_4xx' => false]);
68+
config(['http-client-logger.filtering.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.filter_5xx' => true]);
79+
config(['http-client-logger.filtering.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.filter_5xx' => false]);
87+
config(['http-client-logger.filtering.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.filter_2xx' => false]);
99-
config(['http-client-logger.filter_slow' => 1.5]);
98+
config(['http-client-logger.filtering.2xx' => false]);
99+
config(['http-client-logger.filtering.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.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);
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);
2121
}
2222

2323
protected function getPackageProviders($app)

0 commit comments

Comments
 (0)