Asked  7 Months ago    Answers:  5   Viewed   30 times

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

 Answers

78

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.

use GuzzleHttpHandlerStack;
use GuzzleHttpMiddleware;
use GuzzleHttpMessageFormatter;
use MonologLogger;

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

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

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.

Wednesday, March 31, 2021
 
zIs
answered 7 Months ago
zIs
39

If you want to use JSON in the request, just create it with json_encode():

$request = new Request(
    'PUT',
    'https://api.hitbox.tv/media/live/myName?authToken=myToken',
    ["content-type" => 'application/json'],
    json_encode($params)
);
Wednesday, March 31, 2021
 
jab
answered 7 Months ago
jab
95

Well, when DEBUG = False, Django will automatically mail a full traceback of any error to each person listed in the ADMINS setting, which gets you notifications pretty much for free. If you'd like more fine-grained control, you can write and add to your settings a middleware class which defines a method named process_exception(), which will have access to the exception that was raised:

http://docs.djangoproject.com/en/dev/topics/http/middleware/#process-exception

Your process_exception() method can then perform whatever type of logging you'd like: writing to console, writing to a file, etc., etc.

Edit: though it's a bit less useful, you can also listen for the got_request_exception signal, which will be sent whenever an exception is encountered during request processing:

http://docs.djangoproject.com/en/dev/ref/signals/#got-request-exception

This does not give you access to the exception object, however, so the middleware method is much easier to work with.

Wednesday, June 2, 2021
 
muffe
answered 5 Months ago
61

Though, its not mentioned within the documentation, you can use the "progress" request option.

References to it can be found here.

$options = [
    'progress' => function ($dl_total_size, $dl_size_so_far, $ul_total_size, $ul_size_so_far) {
        // do something.
    }
];
Sunday, July 4, 2021
 
ClmentM
answered 4 Months ago
43

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:

ActiveResource::Base.logger = ActiveRecord::Base.logger

I'm still searching for a better solution that shows me the response and the data posted to update calls, but at least this is a step in the right direction. I'm really not sure why ActiveResource has a separate logger to start with, but that's another matter.

Tuesday, September 14, 2021
 
Pradip
answered 1 Month ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :