php - AWS PHP SDK v3 中的响应日志记录

标签 php guzzle monolog aws-php-sdk guzzle6

在 AWS PHP SDK v2 中,我可以通过简单地执行以下操作来设置请求和响应信息的日志记录:

<?php
use Monolog\Logger;
use Guzzle\Log\MonologLogAdapter;
use Guzzle\Plugin\Log\LogPlugin;
use Aws\S3\S3Client;
$monolog = new Logger('main');
$monolog_adapter = new MonologLogAdapter($monolog);
$log_plugin = new LogPlugin($monolog_adapter);
$s3_client = S3Client::factory(['region' => 'us-east-1']);
$s3_client->addSubscriber($log_plugin);
var_dump($s3_client->doesObjectExist('my-bucket', 'object-that-doesnt-exist'));

# This is the log entry I want in the v3 version:
# [2015-10-30 14:47:20] main.ERROR: myhostname aws-sdk-php2/2.8.20 Guzzle/3.9.3 curl/7.43.0 PHP/5.5.23 - [2015-10-30T14:47:20+00:00] "HEAD /my-bucket/object-that-doesnt-exist HTTP/1.1" 404  ...
# bool(false)

在 v3 中,我似乎找不到解决方案。中间件似乎没有用,因为它们仅在发送请求之前触发,因此我无法访问响应 HTTP 代码。

Guzzle v6 的中间件中内置了此功能,但我不知道如何让它与 aws-php-sdk 一起使用。 https://github.com/guzzle/guzzle/blob/master/src/Middleware.php#L180

我得到的最接近的是这个:

<?php
use Monolog\Logger;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use GuzzleHttp\HandlerStack;
use Aws\S3\S3Client;
$monolog = new Logger('main');
$guzzle_formatter = new MessageFormatter(MessageFormatter::CLF);
$guzzle_log_middleware = Middleware::log($monolog, $guzzle_formatter);
$guzzle_stack = HandlerStack::create();
$guzzle_stack->push($guzzle_log_middleware);
$s3_client = new S3Client([
    'region' => 'us-east-1',
    'version' => '2006-03-01',
    'http_handler' => $guzzle_stack,
]);
var_dump($s3_client->doesObjectExist('my-bucket', 'object-that-doesnt-exist'));

# [2015-10-30 15:10:12] main.INFO: myhostname aws-sdk-php/3.9.2 - [30/Oct/2015:15:10:12 +0000] "HEAD /my-bucket/object-that-doesnt-exist HTTP/1.1" 404  [] []
# bool(true)

但是,虽然日志记录有效,但 doesObjectExist() 现在返回不正确的值,因为此处理程序不会抛出 404 异常,而 aws-php-sdk 预计会发生这种情况。乍一看,其他一些简单的请求(如上传到 S3)似乎可行。不确定此方法还有哪些其他问题。

最佳答案

SDK 中使用的处理程序与 Guzzle 中使用的处理程序略有不同。您正确地创建了一个 Guzzle 处理程序,并且要将其传递到 SDK,您需要像这样创建一个适配器:

<?php

use Aws\Handler\GuzzleV6\GuzzleHandler;
use Aws\S3\S3Client;
use Monolog\Logger;
use GuzzleHttp\Client;
use GuzzleHttp\MessageFormatter;
use GuzzleHttp\Middleware;
use GuzzleHttp\HandlerStack;

$guzzle_stack = HandlerStack::create();
$guzzle_stack->push(Middleware::log(
    new Logger('main'), 
    new MessageFormatter(MessageFormatter::CLF)
));

$handler = new GuzzleHandler(new Client(['handler' => $guzzle_stack]));

$s3_client = new S3Client([
    'region' => 'us-east-1',
    'version' => '2006-03-01',
    'http_handler' => $handler,
]);

var_dump($s3_client->doesObjectExist('my-bucket', 'object-that-doesnt-exist'));

GuzzleHandler 对象将 HTTP 错误转换为异常。

关于php - AWS PHP SDK v3 中的响应日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33439209/

相关文章:

php - 为什么我在尝试使用自定义用户提供程序登录时收到此 "Class App\Listeners\LogAuthenticationAttempt does not exist"错误?

javascript - 通过数据库创建图像上传/下载功能

php - 如何向 Symfony/Monolog 日志输出添加附加信息(主机、URL 等)?

php - symfony monolog 中的 channel 是如何定义的?

php - 如果网址缺少Google Analytics(分析)参数,则进行重定向

php - 值未存储在 mysql 数据库中

php - Laravel/Guzzle - POST 二进制文件内容

php - Laravel 和 Cookie 中的 GuzzleHttp

php - 为什么我的 Guzzle 6 get() 调用返回空流?

symfony - monolog 发送旧日志