Home

Awesome

Laravel HTTP Client Global Logger

Latest Version on Packagist Packagist Downloads PHP from Packagist Build Status GitHub License

A super simple global logger for the Laravel HTTP Client.

Installation

You can install the package via Composer:

$ composer require onlime/laravel-http-client-global-logger

Configuration

This is a zero-configuration package. It is auto-discovered by Laravel and global logging is enabled by default. No further configuration needed - you may skip directly to the Usage section below.

Optionally publish the config file with:

$ php artisan vendor:publish --provider="Onlime\LaravelHttpClientGlobalLogger\Providers\ServiceProvider"

You may override its configuration in your .env - the following environment vars are supported:

(look into config/http-client-global-logger.php for defaults, further configuration, and explanation)

Features

Using the logger will log both the request and response of an external HTTP request made with the Laravel HTTP Client.

Usage

NOTE: For all 3 variants below, you need to keep the HTTP Client Global Logger enabled (not setting HTTP_CLIENT_GLOBAL_LOGGER_ENABLED=false in your .env). The http-client-global-logger.enabled config option is a global on/off switch for all 3 variants, not just the "global" variants. Our project name might be misleading in that context.

Variant 1: Global Logging

Just use Laravel HTTP Client as always - no need to configure anything!

Http::get('https://example.com');

Slightly more complex example:

$client = Http::withOptions([
    'base_uri'        => 'https://example.com',
    'allow_redirects' => false,
]);
$response = $client->get('/user');

Variant 2: Mixin Variant

If you enable mixin variant, global logging will be turned off. Put this into your .env:

HTTP_CLIENT_GLOBAL_LOGGER_MIXIN=true

You could then turn on logging individually on each HTTP Client instance, using the log() method:

Http::log()->get('https://example.com');

Logging with custom channel name (if not specified, defaults to current environment, such as production or local):

Http::log('my-api')->get('https://example.com');

Slightly more complex example:

$client = Http::log('my-api')->withOptions([
    'base_uri'        => 'https://example.com',
    'allow_redirects' => false,
]);
$response = $client->get('/user');

Variant 3: Global HTTP Middleware

If you use Global Middleware (Http::globalRequestMiddleware() and Http::globalResponseMiddleware() methods), you should be aware that Variant 1 uses Laravel's RequestSending event to log HTTP requests. This event is fired before Global Middleware is executed. Therefore, any modifications to the request made by Global Middleware will not be logged. To overcome this, this package provides a middleware that you may add after your Global Middleware.

You may add the middleware using the static addRequestMiddleware() method on the HttpClientLogger class:

use Onlime\LaravelHttpClientGlobalLogger\HttpClientLogger;

HttpClientLogger::addRequestMiddleware();

For example, you may add this to your AppServiceProvider's boot() method after registering your Global Middleware:

use Illuminate\Support\Facades\Http;
use Onlime\LaravelHttpClientGlobalLogger\HttpClientLogger;

Http::globalRequestMiddleware(fn ($request) => $request->withHeader(
    'User-Agent', 'My Custom User Agent'
));

HttpClientLogger::addRequestMiddleware();

Logging example

By default, logs are written to a separate logfile http-client.log.

Log entry example:

[2021-07-11 11:29:58] local.INFO: REQUEST: GET https://example.com/user
GET /user HTTP/1.1
User-Agent: GuzzleHttp/7
Host: example.com
Authorization: Bearer *******************
[2021-07-11 11:29:58] local.INFO: RESPONSE: HTTP/1.1 200 OK
HTTP/1.1 200 OK
Date: Fri, 18 Jun 2021 09:29:58 GMT
Server: nginx
Content-Type: application/json
{"username":"foo","email":"foo@example.com"}

FAQ

How does this package differ from bilfeldt/laravel-http-client-logger ?

Honestly, I did not really look into bilfeldt/laravel-http-client-logger, as my primary goal was to build a global logger for Laravel HTTP Client without any added bulk. Global logging currently (as of July 2021) is still an open issue, see bilfeldt/laravel-http-client-logger#2 - Add global logging.

Both packages provide a different feature set and have those advantages:

So, my recommendation: If you need global logging without any extra configuration and without changing a line of code in your project, go for my package. If you don't want to log everything and wish to filter by HTTP response code, go for Bilfeldt's package. But don't install both!

Caveats

Testing

Currently, there is very basic code/test coverage. We're using PEST, so just run all tests like so:

$ ./vendor/bin/pest

Changes

All changes are listed in CHANGELOG

Authors

Made with ❤️ by Philip Iezzi (Onlime GmbH).

License

This package is licenced under the MIT license however support is more than welcome.