Viewed   100 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

5

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.

Monday, September 19, 2022
3

I see a couple of issues. First is the fact that your $postFields array does not appear to be properly formatted, and second you wrap your $postFields array inside another array.

$options = [
    'debug' => true,
    'form_params' => [
        'test' => 15,
        'id' => 43252435243654352,
        'name' => 'this is a random name',
    ],
    'on_stats' => $someCallableItem,
];
$response = $client->post('api', $options);
Saturday, September 17, 2022
4

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)
);
Sunday, August 14, 2022
 
2
$(element).on("click mousedown mouseup focus blur keydown change",function(e){
     console.log(e);
});

That will get you a lot (but not all) of the information on if an event is fired... other than manually coding it like this, I can't think of any other way to do that.

Saturday, August 6, 2022
1

I think the simplest way to check if the function is ES6 class is to check the result of .toString() method. According to the es2015 spec:

The string representation must have the syntax of a FunctionDeclaration FunctionExpression, GeneratorDeclaration, GeneratorExpression, ClassDeclaration, ClassExpression, ArrowFunction, MethodDefinition, or GeneratorMethod depending upon the actual characteristics of the object

So the check function looks pretty simple:

function isClass(func) {
  return typeof func === 'function' 
    && /^classs/.test(Function.prototype.toString.call(func));
}
Friday, October 7, 2022
 
maxiss
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :