/laravel-http-client-global-logger

A global logger for the Laravel HTTP Client

Primary LanguagePHPMIT LicenseMIT

Laravel HTTP Client Global Logger

Latest Version on Packagist Packagist Downloads PHP from Packagist 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:

  • HTTP_CLIENT_GLOBAL_LOGGER_ENABLED (bool)
  • HTTP_CLIENT_GLOBAL_LOGGER_MIXIN (bool)
  • HTTP_CLIENT_GLOBAL_LOGGER_CHANNEL (string)
  • HTTP_CLIENT_GLOBAL_LOGGER_LOGFILE (string)
  • HTTP_CLIENT_GLOBAL_LOGGER_REQUEST_FORMAT (string)
  • HTTP_CLIENT_GLOBAL_LOGGER_RESPONSE_FORMAT (string)
  • HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_ENABLED (bool)
  • HTTP_CLIENT_GLOBAL_LOGGER_OBFUSCATE_REPLACEMENT (string)

(look into config/http-client-global-logger.php for 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.

  • Multi-line log records that contain full request/response information (including all headers and body)
  • Logging into separate logfile http-client.log. You're free to override this and use your own logging channel or just log to a different logfile.
  • Full support of Guzzle MessageFormatter variable substitutions for highly customized log messages.
  • Variant 1: Global logging (default)
    • Zero-configuration: Global logging is enabled by default in this package.
    • Simple and performant implementation using RequestSending / ResponseReceived event listeners
    • Obfuscation of common credentials passed in request (e.g. Authorization header's Bearer token)
  • Variant 2: Mixin (HTTP_CLIENT_GLOBAL_LOGGER_MIXIN=true)
    • Enabled only on individual HTTP Client instances, using Http::log() - no global logging.
    • Log channel name can be set per HTTP Client instance by passing a name to Http::log($name)

Usage

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');

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

  • This package currently uses two different implementations for logging. In the preferred variant 1 (global logging), it is currently not possible to configure the log channel name which defaults to current environment, such as production or local. If you with to use Laravel HTTP Client to access multiple different external APIs, it is nice to explicitely distinguish between them by different log channel names.

    As a workaround, I have implemented another way of logging through Http::log() method as mixin. But of course, we should combine both variants into a single one for a cleaner codebase.

  • Very basic obfuscation support using regex with lookbehind assertions (e.g. /(?<=Authorization:\sBearer ).*/m, modifying formatted log output. It's currently not possible to directly modify request headers or JSON data in request body.

  • Obfuscation currently only works in variant 1 (global logging).

Testing

TBD.

(any help appreciated!)

Changes

All changes are listed in CHANGELOG

Authors

Author of this shitty little package is Philip Iezzi (Onlime GmbH).

License

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