22

我正在尝试使用 guzzle 6,它工作正常,但在如何记录所有 api 调用时我迷失了。我想简单地记录时间、会话中的登录用户、url 以及与 API 调用有关的任何其他常见相关信息。我似乎找不到 Guzzle 6 的任何文档,只有 guzzle 3(他们更改了日志记录 addSubscriber 调用)。这就是我当前的 API 调用方式:

$client = new GuzzleHttp\Client(['defaults' => ['verify' => false]]);
$res = $client->get($this->url . '/api/details', ['form_params' => ['file' => $file_id]]);
4

3 回答 3

62

您可以在 Guzzle 6 中使用任何实现 PSR-3 接口的记录器

在下面的示例中,我使用 Monolog 作为记录器和带有 MessageFormatter 的 Guzzle 的内置中间件。

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

关于日志中间件和消息格式化程序的详细信息还没有很好的记录。但是您可以查看可以在 MessageFormatter 中使用的变量列表

还有一个guzzle-logmiddleware允许您自定义格式化程序等。

于 2015-09-20T22:24:48.193 回答
8

@KingKongFrog 这是指定日志文件名称的方式

$logger = new Logger('MyLog');
$logger->pushHandler(new StreamHandler(__DIR__ . '/test.log'), Logger::DEBUG);

$stack->push(Middleware::log(
$logger,
new MessageFormatter('{req_body} - {res_body}')
));
于 2017-08-30T08:44:06.967 回答
1

对于 Guzzle 7,我这样做了:

require './guzzle_7.2.0.0/vendor/autoload.php';
require './monolog/vendor/autoload.php';

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;

use GuzzleHttp\MessageFormatter;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;

use GuzzleHttp\TransferStats;

//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
$logger = null;
$messageFormat = 
            //['REQUEST: ', 'METHOD: {method}', 'URL: {uri}', 'HTTP/{version}', 'HEADERS: {req_headers}', 'Payload: {req_body}', 'RESPONSE: ', 'STATUS: {code}', 'BODY: {res_body}'];
            'REQUEST: urldecode(req_body)';
$handlerStack = \GuzzleHttp\HandlerStack::create(); 
$handlerStack->push(createGuzzleLoggingMiddleware($messageFormat));
function getLogger() {
    global $logger;
    if ($logger==null) {
        $logger = new Logger('api-consumer');
        $logger->pushHandler(new \Monolog\Handler\RotatingFileHandler('./TataAigHealthErrorMiddlewarelog.txt'));
    }
    var_dump($logger);
    return $logger;
    }
function createGuzzleLoggingMiddleware(string $messageFormat){
    return \GuzzleHttp\Middleware::log(getLogger(), new \GuzzleHttp\MessageFormatter($messageFormat));
}

function createLoggingHandlerStack(array $messageFormats){  
    global $logger;
    $stack = \GuzzleHttp\HandlerStack::create();
    var_dump($logger);
    collect($messageFormats)->each(function ($messageFormat) use ($stack) {
        // We'll use unshift instead of push, to add the middleware to the bottom of the stack, not the top
        $stack->unshift(createGuzzleLoggingMiddleware($messageFormat) );
    });
    return $stack;
}
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$

$client = new Client(['verify' => false, 'handler' => $tapMiddleware($handlerStack)]);

哇 !!

于 2020-11-02T19:54:21.427 回答