PHPackages                             onlime/laravel-http-client-global-logger - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. onlime/laravel-http-client-global-logger

ActiveLibrary[HTTP &amp; Networking](/categories/http)

onlime/laravel-http-client-global-logger
========================================

A global logger for the Laravel HTTP Client

v1.3.0(1mo ago)1935.1k—5.7%4[1 issues](https://github.com/onlime/laravel-http-client-global-logger/issues)MITPHPPHP ^8.2CI passing

Since Jul 12Pushed 1mo ago1 watchersCompare

[ Source](https://github.com/onlime/laravel-http-client-global-logger)[ Packagist](https://packagist.org/packages/onlime/laravel-http-client-global-logger)[ Docs](https://github.com/onlime/laravel-http-client-global-logger)[ RSS](/packages/onlime-laravel-http-client-global-logger/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (23)Versions (20)Used By (0)

Laravel HTTP Client Global Logger
=================================

[](#laravel-http-client-global-logger)

[![Latest Version on Packagist](https://camo.githubusercontent.com/ad73a5e420ccafcdde9e7fa5b678e8812a9d90f81dedd2dfbb691ee63dcd01ca/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6f6e6c696d652f6c61726176656c2d687474702d636c69656e742d676c6f62616c2d6c6f676765722e737667)](https://packagist.org/packages/onlime/laravel-http-client-global-logger)[![Packagist Downloads](https://camo.githubusercontent.com/9efb60cb5009e4901b8487cf972193df3126dd32591ab2436913634439bdb8a4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6f6e6c696d652f6c61726176656c2d687474702d636c69656e742d676c6f62616c2d6c6f676765722e737667)](https://packagist.org/packages/onlime/laravel-http-client-global-logger)[![PHP from Packagist](https://camo.githubusercontent.com/0b1fe0882137cefe18ec2cec535d6c5e7baf06b5ea293a4fdbdf272ac6b7ab3c/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f6f6e6c696d652f6c61726176656c2d687474702d636c69656e742d676c6f62616c2d6c6f676765722e737667)](https://packagist.org/packages/onlime/laravel-http-client-global-logger)[![Build Status](https://github.com/onlime/laravel-http-client-global-logger/actions/workflows/ci.yml/badge.svg)](https://github.com/onlime/laravel-http-client-global-logger/actions/workflows/ci.yml)[![GitHub License](https://camo.githubusercontent.com/7fefb4ddf81f1708e211159a1f8d255a80c1aea28988a29676ebfbcc99184be0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f6f6e6c696d652f6c61726176656c2d687474702d636c69656e742d676c6f62616c2d6c6f676765722e737667)](https://github.com/onlime/laravel-http-client-global-logger/blob/main/LICENSE)

A super simple global logger for the [Laravel HTTP Client](https://laravel.com/docs/http-client).

Installation
------------

[](#installation)

You can install the package via Composer:

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

Configuration
-------------

[](#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](#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)
- `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_ENABLED` (bool)
- `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_LIMIT` (int)
- `HTTP_CLIENT_GLOBAL_LOGGER_TRIM_RESPONSE_BODY_CONTENT_TYPE_WHITELIST` (string)

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

Features
--------

[](#features)

Using the logger will log both the request and response of an external HTTP request made with the [Laravel HTTP Client](https://laravel.com/docs/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](https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php) variable substitutions for highly customized log messages.
- Basic obfuscation of credentials in HTTP Client requests (both by header or body keys)
- Trimming of response body content to a certain length with support for `Content-Type` whitelisting
- Enforce trimming of response body content by setting a `X-Global-Logger-Trim-Always` request header, which will ignore the whitelisting.
- **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)`
- **Variant 3: Global HTTP Middleware**
    - Can be used in combination with other `Http::globalRequestMiddleware()` calls in your `AppServiceProvider`'s `boot()` method, after registering your [Global Middleware](https://laravel.com/docs/10.x/http-client#global-middleware).

Usage
-----

[](#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

[](#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

[](#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

[](#variant-3-global-http-middleware)

If you use [Global Middleware](https://laravel.com/docs/10.x/http-client#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
---------------

[](#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
---

[](#faq)

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

[](#how-does-this-package-differ-from-bilfeldtlaravel-http-client-logger-)

Honestly, I did not really look into [bilfeldt/laravel-http-client-logger](https://github.com/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](https://github.com/bilfeldt/laravel-http-client-logger/issues/2).

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

- [onlime/laravel-http-client-global-logger](https://github.com/onlime/laravel-http-client-global-logger) (this package)
    - global logging
    - auto-configured log channel `http-client` to log to a separate `http-client.log` file
    - Full support of [Guzzle MessageFormatter](https://github.com/guzzle/guzzle/blob/master/src/MessageFormatter.php) variable substitutions for highly customized log messages.
    - basic obfuscation of credentials in HTTP Client requests
    - trimming of response body content
- [bilfeldt/laravel-http-client-logger](https://github.com/bilfeldt/laravel-http-client-logger)
    - conditional logging using `logWhen($condition)`
    - filtering of logs by HTTP response codes
    - currently still supports PHP 7.4+

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](https://github.com/bilfeldt)'s package. **But don't install both!**

Caveats
-------

[](#caveats)

- This package currently uses two different implementations for logging. In the preferred variant 1 or 3 (global logging), it is currently not possible to configure the [log channel name](https://laravel.com/docs/logging#configuring-the-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 explicitly 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.
- Obfuscation

    - Body keys: Very basic obfuscation support using regex with lookbehind assertions (e.g. `/(?
