Skip to content
Open
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
12 changes: 12 additions & 0 deletions config/http-client-logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@
*/
'enabled' => env('HTTP_CLIENT_LOGGER_ENABLED', true),

/*
|--------------------------------------------------------------------------
| Global logging
|--------------------------------------------------------------------------
|
| Whether or not logging should be globally enabled when using the option
| specified below. Filters will stil be applied at runtime and will not
| override this setting.
|
*/
'global' => env('HTTP_CLIENT_GLOBAL_LOGGING_ENABLED', false),

/*
|--------------------------------------------------------------------------
| Filtering options
Expand Down
27 changes: 27 additions & 0 deletions src/Listeners/LogRequestSending.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bilfeldt\LaravelHttpClientLogger\Listeners;

use GuzzleHttp\MessageFormatter;
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Support\Facades\Log;
use Psr\Http\Message\RequestInterface;

class LogRequestSending
{
/**
* Handle the event.
*
* @param RequestSending $event
* @return void
*/
public function handle(RequestSending $event)
{
return $event->request->withMiddleware((new LoggingMiddleware(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Can we just use app(LoggingMiddleware::class) instead?

$logger ?? app(HttpLoggerInterface::class),
$filter ?? app(HttpLoggingFilterInterface::class)
))->__invoke($context = [], $config = []));
}


}
25 changes: 25 additions & 0 deletions src/Listeners/LogResponseReceived.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Bilfeldt\LaravelHttpClientLogger\Listeners;

use GuzzleHttp\MessageFormatter;
use Illuminate\Http\Client\Events\ResponseReceived;
use Illuminate\Support\Facades\Log;
use Bilfeldt\LaravelHttpClientLogger\Middleware\LoggingMiddleware;

class LogResponseReceived
{
/**
* Handle the event.
*
* @param ResponseReceived $event
* @return void
*/
public function handle(ResponseReceived $event)
{
$event->response->withMiddleware((new LoggingMiddleware(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Can we just use app(LoggingMiddleware::class) instead?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I can't see why note. That would make sense to resolve it rather than instantiating it again ☺️

$logger ?? app(HttpLoggerInterface::class),
$filter ?? app(HttpLoggingFilterInterface::class)
))->__invoke($context = [], $config = []));
}
}
36 changes: 36 additions & 0 deletions src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Bilfeldt\LaravelHttpClientLogger\Providers;

use Bilfeldt\LaravelHttpClientLogger\Listeners\LogRequestSending;
use Bilfeldt\LaravelHttpClientLogger\Listeners\LogResponseReceived;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as BaseEventServiceProvider;
use Illuminate\Http\Client\Events\RequestSending;
use Illuminate\Http\Client\Events\ResponseReceived;

class EventServiceProvider extends BaseEventServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
RequestSending::class => [
LogRequestSending::class,
],
ResponseReceived::class => [
LogResponseReceived::class,
],
];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we test this using a simple unit test that uses assertListening? 🤔

Event::assertListening(
    RequestSending::class,
    LogRequestSending::class
);

Event::assertListening(
    ResponseReceived::class,
    LogRequestSending::class
);

See:


/**
* Get the events and handlers.
*
* @return array
*/
public function listens()
{
return config('http-client-logger.global') ? $this->listen : [];
}
}