Skip to content

Commit 0fc36d4

Browse files
Init commit
1 parent 1fe0a62 commit 0fc36d4

12 files changed

+397
-21
lines changed

.editorconfig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
root = true
2+
3+
[*]
4+
charset = utf-8
5+
end_of_line = lf
6+
insert_final_newline = true
7+
indent_style = space
8+
indent_size = 4
9+
trim_trailing_whitespace = true
10+
11+
[*.md]
12+
trim_trailing_whitespace = false
13+
14+
[*.{yml,yaml,json}]
15+
indent_size = 2

.gitattributes

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
* text=auto
2+
3+
*.md diff=markdown
4+
*.php diff=php
5+
6+
/tests export-ignore
7+
.editorconfig export-ignore
8+
.gitattributes export-ignore
9+
.gitignore export-ignore
10+
phpunit.xml export-ignore

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/.idea
2+
/vendor/

LICENSE

Lines changed: 0 additions & 21 deletions
This file was deleted.

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Laravel Http Client Logger
2+
3+
Laravel Http Client Logger - library for logging connection/request/response actions for Laravel HTTP Client
4+
5+
## Installation
6+
7+
You can install the package via composer:
8+
9+
```bash
10+
composer require alexeyshchetkin/laravel-http-client-logger
11+
```
12+
13+
You can publish the config file with:
14+
15+
```bash
16+
php artisan vendor:publish --provider="AlexeyShchetkin\LaravelHttpClientLogger\Providers\HttpClientLoggerServiceProvider" --tag="laravel-http-client-logger"
17+
```
18+
19+
The contents of the published config file:
20+
21+
```php
22+
return [
23+
/*
24+
* Channel name for logging
25+
*/
26+
'channel_name' => 'external_requests',
27+
/*
28+
* Channel log level
29+
*/
30+
'log_level' => 'info',
31+
/*
32+
* Channel configuration in Laravel style
33+
*/
34+
'channel_configuration' => [
35+
'driver' => 'daily',
36+
'path' => storage_path('logs/external_requests.log'),
37+
'formatter' => JsonFormatter::class,
38+
'level' => env('LOG_LEVEL', 'info'),
39+
'days' => 7,
40+
],
41+
/*
42+
* Http client events for logging (comment not needed, add own if needed)
43+
*/
44+
'events' => [
45+
ConnectionFailed::class => ConnectionFailedListener::class,
46+
RequestSending::class => RequestSendingListener::class,
47+
ResponseReceived::class => ResponseReceivedListener::class,
48+
],
49+
];
50+
```
51+
52+
## License
53+
54+
The MIT License (MIT).

composer.json

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"name": "alexeyshchetkin/laravel-http-client-logger",
3+
"version": "1.0",
4+
"type": "library",
5+
"require": {
6+
"php": "^7|^8",
7+
"illuminate/support": ">=8"
8+
},
9+
"license": "MIT",
10+
"autoload": {
11+
"psr-4": {
12+
"AlexeyShchetkin\\LaravelHttpClientLogger\\": "src/"
13+
}
14+
},
15+
"autoload-dev": {
16+
"psr-4": {
17+
"AlexeyShchetkin\\LaravelHttpClientLogger\\": "tests/"
18+
}
19+
},
20+
"authors": [
21+
{
22+
"name": "Alexey Shchetkin",
23+
"email": "[email protected]"
24+
}
25+
],
26+
"extra": {
27+
"laravel": {
28+
"providers": [
29+
"AlexeyShchetkin\\LaravelHttpClientLogger\\Providers\\HttpClientLoggerServiceProvider"
30+
]
31+
}
32+
}
33+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use AlexeyShchetkin\LaravelHttpClientLogger\Listeners\ConnectionFailedListener;
4+
use AlexeyShchetkin\LaravelHttpClientLogger\Listeners\RequestSendingListener;
5+
use AlexeyShchetkin\LaravelHttpClientLogger\Listeners\ResponseReceivedListener;
6+
use Illuminate\Http\Client\Events\ConnectionFailed;
7+
use Illuminate\Http\Client\Events\RequestSending;
8+
use Illuminate\Http\Client\Events\ResponseReceived;
9+
use Monolog\Formatter\JsonFormatter;
10+
11+
return [
12+
/*
13+
* Channel name for logging
14+
*/
15+
'channel_name' => 'external_requests',
16+
/*
17+
* Channel log level
18+
*/
19+
'log_level' => 'info',
20+
/*
21+
* Channel configuration in Laravel style
22+
*/
23+
'channel_configuration' => [
24+
'driver' => 'daily',
25+
'path' => storage_path('logs/external_requests.log'),
26+
'formatter' => JsonFormatter::class,
27+
'level' => env('LOG_LEVEL', 'info'),
28+
'days' => 7,
29+
],
30+
/*
31+
* Http client events for logging (comment not needed, add own if needed)
32+
*/
33+
'events' => [
34+
ConnectionFailed::class => ConnectionFailedListener::class,
35+
RequestSending::class => RequestSendingListener::class,
36+
ResponseReceived::class => ResponseReceivedListener::class,
37+
],
38+
];
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlexeyShchetkin\LaravelHttpClientLogger\Listeners;
6+
7+
use Illuminate\Http\Client\Events\ConnectionFailed;
8+
use Illuminate\Http\Client\Request;
9+
use Illuminate\Support\Facades\Log;
10+
use AlexeyShchetkin\LaravelHttpClientLogger\ValueObjects\HttpClientLoggerConfiguration;
11+
12+
class ConnectionFailedListener
13+
{
14+
private HttpClientLoggerConfiguration $configuration;
15+
16+
public function __construct(HttpClientLoggerConfiguration $configuration)
17+
{
18+
$this->configuration = $configuration;
19+
}
20+
21+
public function handle(ConnectionFailed $event)
22+
{
23+
$request = $event->request;
24+
rescue(
25+
fn() => Log::channel($this->configuration->getChannelName())->log(
26+
$this->configuration->getLogLevel(),
27+
'Connection failed for url: ' . $request->url(),
28+
$this->generateLogMessage($request),
29+
)
30+
);
31+
}
32+
33+
private function generateLogMessage(Request $request): array
34+
{
35+
return [
36+
'url' => $request->url(),
37+
'method' => $request->method(),
38+
'request' => [
39+
'headers' => $request->headers(),
40+
'payload' => $request->data(),
41+
],
42+
];
43+
}
44+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlexeyShchetkin\LaravelHttpClientLogger\Listeners;
6+
7+
use Illuminate\Http\Client\Events\RequestSending;
8+
use Illuminate\Http\Client\Request;
9+
use Illuminate\Support\Facades\Log;
10+
use AlexeyShchetkin\LaravelHttpClientLogger\ValueObjects\HttpClientLoggerConfiguration;
11+
12+
class RequestSendingListener
13+
{
14+
private HttpClientLoggerConfiguration $configuration;
15+
16+
public function __construct(HttpClientLoggerConfiguration $configuration)
17+
{
18+
$this->configuration = $configuration;
19+
}
20+
21+
public function handle(RequestSending $event)
22+
{
23+
$request = $event->request;
24+
25+
rescue(
26+
fn() => Log::channel($this->configuration->getChannelName())->log(
27+
$this->configuration->getLogLevel(),
28+
'Request for url: ' . $request->url(),
29+
$this->generateLogMessage($request),
30+
)
31+
);
32+
}
33+
34+
private function generateLogMessage(Request $request): array
35+
{
36+
return [
37+
'url' => $request->url(),
38+
'method' => $request->method(),
39+
'request' => [
40+
'headers' => $request->headers(),
41+
'payload' => $request->data(),
42+
],
43+
];
44+
}
45+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AlexeyShchetkin\LaravelHttpClientLogger\Listeners;
6+
7+
use Illuminate\Http\Client\Events\ResponseReceived;
8+
use Illuminate\Http\Client\Request;
9+
use Illuminate\Http\Client\Response;
10+
use Illuminate\Support\Facades\Log;
11+
use AlexeyShchetkin\LaravelHttpClientLogger\ValueObjects\HttpClientLoggerConfiguration;
12+
13+
class ResponseReceivedListener
14+
{
15+
private HttpClientLoggerConfiguration $configuration;
16+
17+
public function __construct(HttpClientLoggerConfiguration $configuration)
18+
{
19+
$this->configuration = $configuration;
20+
}
21+
22+
public function handle(ResponseReceived $event)
23+
{
24+
$request = $event->request;
25+
$response = $event->response;
26+
27+
rescue(
28+
fn() => Log::channel($this->configuration->getChannelName())->log(
29+
$this->configuration->getLogLevel(),
30+
'Response for url: ' . $request->url(),
31+
$this->generateLogMessage($request, $response),
32+
)
33+
);
34+
}
35+
36+
private function generateLogMessage(Request $request, Response $response): array
37+
{
38+
return [
39+
'url' => $request->url(),
40+
'method' => $request->method(),
41+
'status' => $response->status(),
42+
'request' => [
43+
'headers' => $request->headers(),
44+
'payload' => $request->data(),
45+
],
46+
'response' => [
47+
'headers' => $response->headers(),
48+
'namelookup_time' => $response->handlerStats()['namelookup_time'],
49+
'pretransfer_time' => $response->handlerStats()['pretransfer_time'],
50+
'starttransfer_time' => $response->handlerStats()['starttransfer_time'],
51+
'connect_time' => $response->handlerStats()['connect_time'],
52+
'total_time' => $response->handlerStats()['total_time'],
53+
'redirect_time' => $response->handlerStats()['redirect_time'],
54+
'redirect_count' => $response->handlerStats()['redirect_count'],
55+
'size_download' => $response->handlerStats()['size_download'],
56+
],
57+
];
58+
}
59+
}

0 commit comments

Comments
 (0)