Skip to content
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

feat: Add HTTP Client Logger #28

Merged
merged 1 commit into from
Apr 7, 2024
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
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ Debug log output is disabled by default, so please enable it in `.env`.

```
LOG_QUERY_ENABLED=true
LOG_REQUEST_ENABLED=true
LOG_RESPONSE_ENABLED=true
LOG_HTTP_REQUEST_ENABLED=true
LOG_HTTP_RESPONSE_ENABLED=true
LOG_HTTP_CLIENT_REQUEST_ENABLED=true
LOG_HTTP_CLIENT_RESPONSE_ENABLED=true
```

### Option config file publish
Expand Down
26 changes: 24 additions & 2 deletions config/blink-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
|--------------------------------------------------------------------------
*/
'request' => [
'enabled' => env('LOG_REQUEST_ENABLED', false),
'enabled' => env('LOG_HTTP_REQUEST_ENABLED', false),
'channel' => config('logging.default'),
'include_paths' => [],
'exclude_paths' => [],
Expand All @@ -44,7 +44,7 @@
|--------------------------------------------------------------------------
*/
'response' => [
'enabled' => env('LOG_RESPONSE_ENABLED', false),
'enabled' => env('LOG_HTTP_RESPONSE_ENABLED', false),
'channel' => config('logging.default'),
'include_paths' => [],
'exclude_paths' => [],
Expand All @@ -54,4 +54,26 @@
],
],
],

'http_client' => [
/*
|--------------------------------------------------------------------------
| Request Logger
|--------------------------------------------------------------------------
*/
'request' => [
'enabled' => env('LOG_HTTP_CLIENT_REQUEST_ENABLED', false),
'channel' => config('logging.default'),
],

/*
|--------------------------------------------------------------------------
| Response Logger
|--------------------------------------------------------------------------
*/
'response' => [
'enabled' => env('LOG_HTTP_CLIENT_RESPONSE_ENABLED', false),
'channel' => config('logging.default'),
],
],
];
34 changes: 34 additions & 0 deletions src/Listeners/RequestSendingLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace LaravelBlinkLogger\Listeners;

use Illuminate\Config\Repository;
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Log\LogManager;
use Psr\Log\LoggerInterface;

class RequestSendingLogger
{
/**
* @param LogManager $logger
*/
public function __construct(
private LoggerInterface $logger,
private Repository $config,
) {
}

public function handle(RequestSending $event): void
{
$this->logger->channel($this->config->get('blink-logger.http_client.response.channel'))->debug(sprintf(
'%s: %s',
$event->request->method(),
$event->request->url(),
), [
'body' => $event->request->data(),
'headers' => $event->request->headers(),
]);
}
}
41 changes: 41 additions & 0 deletions src/Listeners/ResponseReceivedLogger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace LaravelBlinkLogger\Listeners;

use Illuminate\Config\Repository;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Http\Client\Response;
use Illuminate\Log\LogManager;
use Illuminate\Support\Str;
use Psr\Log\LoggerInterface;

class ResponseReceivedLogger
{
/**
* @param LogManager $logger
*/
public function __construct(
private LoggerInterface $logger,
private Repository $config,
) {
}

public function handle(ResponseReceived $event): void
{
$this->logger->channel($this->config->get('blink-logger.http_client.response.channel'))->debug(sprintf(
'%d %s',
$event->response->status(),
$event->response->reason(),
), [
'body' => $this->isJson($event->response) ? $event->response->json() : $event->response->body(),
'headers' => $event->response->headers(),
]);
}

private function isJson(Response $response): bool
{
return Str::startsWith($response->headers()['Content-Type'][0] ?? '', 'application/json');
}
}
18 changes: 16 additions & 2 deletions src/Providers/LaravelBlinkLoggerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@
use Illuminate\Database\Events\TransactionBeginning;
use Illuminate\Database\Events\TransactionCommitted;
use Illuminate\Database\Events\TransactionRolledBack;
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Routing\Router;
use Illuminate\Support\ServiceProvider;
use LaravelBlinkLogger\Listeners\QueryExecutedLogger;
use LaravelBlinkLogger\Listeners\RequestSendingLogger;
use LaravelBlinkLogger\Listeners\ResponseReceivedLogger;
use LaravelBlinkLogger\Listeners\TransactionBeginningLogger;
use LaravelBlinkLogger\Listeners\TransactionCommittedLogger;
use LaravelBlinkLogger\Listeners\TransactionRolledBackLogger;
Expand All @@ -33,7 +37,7 @@ public function boot(Repository $config, Dispatcher $events, Router $router): vo
$events->listen(TransactionRolledBack::class, TransactionRolledBackLogger::class);
}

// Request Logger
// HTTP Request Logger
if ($config->get('blink-logger.http.request.enabled')) {
$middleware = $config->get('blink-logger.http.request.middleware');
$middlewareGroupNames = $config->get('blink-logger.http.request.middleware_group_names');
Expand All @@ -42,14 +46,24 @@ public function boot(Repository $config, Dispatcher $events, Router $router): vo
}
}

// Response Logger
// HTTP Response Logger
if ($config->get('blink-logger.http.response.enabled')) {
$middleware = $config->get('blink-logger.http.response.middleware');
$middlewareGroupNames = $config->get('blink-logger.http.response.middleware_group_names');
foreach ($middlewareGroupNames as $middlewareGroupName) {
$router->pushMiddlewareToGroup($middlewareGroupName, $middleware);
}
}

// HTTP Client Request Logger
if ($config->get('blink-logger.http_client.request.enabled')) {
$events->listen(RequestSending::class, RequestSendingLogger::class);
}

// HTTP Client Response Logger
if ($config->get('blink-logger.http_client.response.enabled')) {
$events->listen(ResponseReceived::class, ResponseReceivedLogger::class);
}
}

public function register(): void
Expand Down