Blog Detail


A Very Flexible Logger for the Laravel HTTP Client cover image

arrow_back A Very Flexible Logger for the Laravel HTTP Client

Bilfeldt introduced an HTTP Client Logger Package that is a very easy and flexible logger for the built-in Laravel HTTP client. It makes logging the request and response simple by sending it to a configured Laravel logger.
This package makes use of Laravels package auto-discovery mechanism so there is no need to do any further steps. If for some reason you wish to opt-out of package auto-discovery, check the Laravel Docs for more details.


You can install the package via composer:

composer require bilfeldt/laravel-http-client-logger


Optionally in Laravel publish the config file with:

php artisan vendor:publish --provider="Bilfeldt\LaravelHttpClientLogger\LaravelHttpClientLoggerServiceProvider" --tag="laravel-http-client-logger-config"


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

Basic logging

Http::log()->get(''); // uses the configured logger and filter

Conditional logging

This will log the request/response when the $condition evaluates to true.

Http::logWhen($condition)->get(''); // uses the configured logger and filter

Logging context

It is possible to supply context for the logger using:

Http::log(['note' => 'Something to log'])->get('');
// or
Http::logWhen($condition, ['note' => 'Something to log'])->get('');

Providing on-demand configuration

It is possible to provide an on-demand configuration that will override the package configuration specified in config/laravel-http-client-logger.php:

Http::log($context, ['example-config-key' => 'value'])->get('');
// or
Http::logWhen($condition, $context, ['example-config-key' => 'value'])->get('');

Specifying a logger

The default logger and filter are specified in the package configuration logger and filter respectively but can be changed at runtime using:

Http::log($context, $config, $logger, $filter)->get('');
// or
Http::logWhen($condition, $context, $config, $logger, $filter)->get('');
// or the shorthand syntax which will use the LogAllFilter

Note that the logger must implement HttpLoggerInterface while the filter must implement HttpLoggingFilterInterface.

Logging Example

The default logger converts the request and response to a PSR-7 HTTP message which is then logged as strings.

Log entry example when using default logger:


//[2021-03-08 06:58:49] local.DEBUG: Time 0.12105202674866sec
//GET /p2/bilfeldt/laravel-http-client-logger.json HTTP/1.1
//User-Agent: GuzzleHttp/7
//HTTP/1.1 200 OK
//Server: nginx
//Date: Mon, 08 Mar 2021 06:58:49 GMT
//Content-Type: application/json
//Last-Modified: Wed, 17 Feb 2021 14:31:03 GMT
//Transfer-Encoding: chunked
//Connection: keep-alive
//Vary: Accept-Encoding
//{"packages":{"bilfeldt/laravel-http-client-logger":[{"name":"bilfeldt/laravel-http-client-logger","description":"A logger for the Laravel HTTP Client","keywords":["bilfeldt","laravel-http-client-logger"],"homepage":"","version":"v0.2.0","version_normalized":"","license":["MIT"],"authors":[{"name":"Anders Bilfeldt","email":"","role":"Developer"}],"source":{"type":"git","url":"","reference":"67ea252a3d3d0c9c0e1c7daa11a3683db818ad5e"},"dist":{"type":"zip","url":"","reference":"67ea252a3d3d0c9c0e1c7daa11a3683db818ad5e","shasum":""},"type":"library","time":"2021-02-17T14:28:45+00:00","autoload":{"psr-4":{"Bilfeldt\\LaravelHttpClientLogger\\":"src"}},"extra":{"laravel":{"providers":["Bilfeldt\\LaravelHttpClientLogger\\LaravelHttpClientLoggerServiceProvider"]}},"require":{"php":"^7.4|^8.0","guzzlehttp/guzzle":"^7.2","illuminate/http":"^8.0","illuminate/support":"^8.0","spatie/laravel-package-tools":"^1.1"},"require-dev":{"orchestra/testbench":"^6.0","phpunit/phpunit":"^9.3","spatie/laravel-ray":"^1.12","timacdonald/log-fake":"^1.9","vimeo/psalm":"^4.4"},"support":{"issues":"","source":""}},{"version":"0.1.0","version_normalized":"","source":{"type":"git","url":"","reference":"6bb8c8ada3959643103a75aa4e639c8dddddf2df"},"dist":{"type":"zip","url":"","reference":"6bb8c8ada3959643103a75aa4e639c8dddddf2df","shasum":""},"time":"2021-02-15T22:39:05+00:00","support":{"issues":"","source":""}}]},"minified":"composer/2.0"}  

For more details about this package, Please visit its documentation & source code on Github.

Published at : 26-01-2022

Author : Rizwan Aslam
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project