Laravel HTTP Client Logger

Packages

June 3rd, 2021

laravel-http-logger-featured.png

HTTP Client Logger is a logger for the built-in Laravel HTTP client. It makes logging the request and response simple by sending it to a configured Laravel logger.

In its simplest form, this Logger package will output the following example log:

1Http::log()->get('https://repo.packagist.org/p2/bilfeldt/laravel-http-client-logger.json');
2
3//[2021-03-08 06:58:49] local.DEBUG: Time 0.12105202674866sec
4//Request
5//GET /p2/bilfeldt/laravel-http-client-logger.json HTTP/1.1
6//User-Agent: GuzzleHttp/7
7//Host: repo.packagist.org
8//
9//
10//Response
11//HTTP/1.1 200 OK
12//Server: nginx
13//Date: Mon, 08 Mar 2021 06:58:49 GMT
14//Content-Type: application/json
15//Last-Modified: Wed, 17 Feb 2021 14:31:03 GMT
16//Transfer-Encoding: chunked
17//Connection: keep-alive
18//Vary: Accept-Encoding
19//
20//{"packages":{"bilfeldt/laravel-http-client-logger":[...]}}

You can also conditionally log HTTP requests based on a given “truthy” condition using the logWhen() and even supply logging context to both log() and logWhen():

1// Log some context along with the HTTP request log
2Http::log(['note' => 'Something to log'])->get('https://example.com');
3
4// Log (with context) when $condition === true
5Http::logWhen($condition, ['note' => 'Something to log'])
6 ->get('https://example.com');

Additionally, this package allows you to pass on-demand logging configuration and even specify a logger for complete HTTP logging flexibility.

This implementation is made possible through the macroable PendingRequest class you can see in action here.

You can learn more about this package, get full installation instructions, and view the source code on GitHub.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.