How do you log all API calls using Guzzle 6

Asked
Active3 hr before
Viewed126 times

9 Answers

calls
90%

I'm trying to use guzzle 6 which works fine but I'm lost when it comes to how to log all the api calls. I would like to simply log timing, logged in user from session, url and any other usual pertinent info that has to do with the API call. I can't seem to find any documentation for Guzzle 6 that refers to this, only guzzle 3 (Where they've changed the logging addSubscriber call). This is how my current API calls are:,I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,The details about the log middleware and message formatter has not well documented yet. But you can check the list which variables you can use in MessageFormatter

I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example.

use GuzzleHttp\ HandlerStack;
use GuzzleHttp\ Middleware;
use GuzzleHttp\ MessageFormatter;
use Monolog\ Logger;

$stack = HandlerStack::create();
$stack - > push(
   Middleware::log(
      new Logger('Logger'),
      new MessageFormatter('{req_body} - {res_body}')
   )
);
$client = new\ GuzzleHttp\ Client(
   [
      'base_uri' => 'http://httpbin.org',
      'handler' => $stack,
   ]
);

echo(string) $client - > get('ip') - > getBody();
load more v
88%

I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example.,You can use any logger which implements PSR-3 interface with Guzzle 6,The details about the log middleware and message formatter has not well documented yet. But you can check the list which variables you can use in MessageFormatter,Also there is a guzzle-logmiddleware which allows you to customize formatter etc.

I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example.

use GuzzleHttp\ HandlerStack;
use GuzzleHttp\ Middleware;
use GuzzleHttp\ MessageFormatter;
use Monolog\ Logger;

$stack = HandlerStack::create();
$stack - > push(
   Middleware::log(
      new Logger('Logger'),
      new MessageFormatter('{req_body} - {res_body}')
   )
);
$client = new\ GuzzleHttp\ Client(
   [
      'base_uri' => 'http://httpbin.org',
      'handler' => $stack,
   ]
);

echo(string) $client - > get('ip') - > getBody();
load more v
72%

The $handlerStack will eventually hold the middleware that handles the logging.,The way to do this is to pass a new HandlerStack with our logging middleware into our Client instance.,https://github.com/rtheunissen/guzzle-log-middleware,And there we have it! Now, adding multiple loggers to our Guzzle Client is as easy as the code below:

The syntax for creating a new Guzzle instance is as follows:

$client = new\ GuzzleHttp\ Client([
'base_uri' => "http://www.example.com/api",
'handler' => $handlerStack,
]), ]);
load more v
65%

I'm trying to use guzzle 6 which works fine but I'm lost when it comes to how to log all the api calls. I would like to simply log timing, logged in user from session, url and any other usual pertinent info that has to do with the API call. I can't seem to find any documentation for Guzzle 6 that refers to this, only guzzle 3 (Where they've changed the logging addSubscriber call). This is how my current API calls are:,Also there is a guzzle-logmiddleware which allows you to customize formatter etc. ,You can use any logger which implements PSR-3 interface with Guzzle 6,If you add the following line to your environment.rb file, it will at least log the requests so you know that URLs ActiveResource is hitting:

I'm trying to use guzzle 6 which works fine but I'm lost when it comes to how to log all the api calls. I would like to simply log timing, logged in user from session, url and any other usual pertinent info that has to do with the API call. I can't seem to find any documentation for Guzzle 6 that refers to this, only guzzle 3 (Where they've changed the logging addSubscriber call). This is how my current API calls are:

$client = new GuzzleHttpClient(['defaults' => ['verify' => false]]);
$res = $client - > get($this - > url.
   '/api/details', ['form_params' => ['file' => $file_id]]);
load more v
75%

I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example.,You can use any logger which implements PSR-3 interface with Guzzle 6,The details about the log middleware and message formatter has not well documented yet. But you can check the list which variables you can use in MessageFormatter,Also there is a guzzle-logmiddleware which allows you to customize formatter etc.

I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example.

use GuzzleHttp\ HandlerStack;
use GuzzleHttp\ Middleware;
use GuzzleHttp\ MessageFormatter;
use Monolog\ Logger;

$stack = HandlerStack::create();
$stack - > push(
   Middleware::log(
      new Logger('Logger'),
      new MessageFormatter('{req_body} - {res_body}')
   )
);
$client = new\ GuzzleHttp\ Client(
   [
      'base_uri' => 'http://httpbin.org',
      'handler' => $stack,
   ]
);

echo(string) $client - > get('ip') - > getBody();
40%

From now on each request and response you execute using $client object will be logged. By default the middleware logs every activity with level DEBUG.,From now on all Requests will be logged. Note that at the example above only requests are logged.,This is a middleware for guzzle that will help you automatically log every request and response using a PSR-3 logger.,handler - A HandlerInterface class that will be responsible for logging your request/response. Check Handlers sections.

$ composer require gmponos / guzzle_logger
load more v
22%

Setup the Logging Handler Stack,Here, I set up a helper function “setLoggingHandler” to create a HandlerStack with all the required logging middleware from an array of message format strings that I would like to log.,I’ll set up a helper function “get_logger” to create the logger:,I’ll set up a helper function “setGuzzleMiddleware” to create the middleware:

You can use the following artisan command to create a service provider.

php artisan make: provider GuzzleClientServiceProvider
load more v
60%

You can use any logger which implements PSR-3 interface with Guzzle 6,I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example.,Also there is a guzzle-logmiddleware which allows you to customize formatter etc., Call all classes that implement an interface in Symfony

I used Monolog as logger and builtin middleware of Guzzle with MessageFormatter in below example.

use GuzzleHttp\ HandlerStack;
use GuzzleHttp\ Middleware;
use GuzzleHttp\ MessageFormatter;
use Monolog\ Logger;
$stack = HandlerStack::create();
$stack - > push(
   Middleware::log(
      new Logger('Logger'),
      new MessageFormatter('{req_body} - {res_body}')
   )
);
$client = new\ GuzzleHttp\ Client(
   [
      'base_uri' => 'http://httpbin.org',
      'handler' => $stack,
   ]
);
echo(string) $client - > get('ip') - > getBody();
48%

Guzzle is a PHP HTTP client that makes it easy to send HTTP requests and trivial to integrate with web services.,Simple interface for building query strings, POST requests, streaming large uploads, streaming large downloads, using HTTP cookies, uploading JSON data, etc...,Uses PSR-7 interfaces for requests, responses, and streams. This allows you to utilize other PSR-7 compatible libraries with Guzzle.,Testing Guzzle Clients Mock Handler History Middleware Test Web Server Using the test server

$client = new GuzzleHttp\ Client();
$res = $client - > request('GET', 'https://api.github.com/user', [
   'auth' => ['user', 'pass']
]);
echo $res - > getStatusCode();
// "200"
echo $res - > getHeader('content-type')[0];
// 'application/json; charset=utf8'
echo $res - > getBody();
// {"type":"User"...'

// Send an asynchronous request.
$request = new\ GuzzleHttp\ Psr7\ Request('GET', 'http://httpbin.org');
$promise = $client - > sendAsync($request) - > then(function($response) {
   echo 'I completed! '.$response - > getBody();
});
$promise - > wait();

Other "calls-undefined" queries related to "How do you log all API calls using Guzzle 6"