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

AWS PHP SDK v3中的响应日志记录,php,guzzle,monolog,aws-php-sdk,guzzle6,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); $l

在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)

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

粗略地看一下S3Client对象是如何实例化的,这表明使用v3发送的请求与使用v6发送的请求不同。工厂声明不包括“版本”选项。这可能会也可能不会有什么不同。此外,“debug”=true选项在这种情况下总是有用的。V3需要提供版本,而V2不需要。S3API只有一个版本,所以没有版本选项也不会有什么不同。虽然我提交了一份关于“$new”拼写错误的编辑,但这还是有效的。谢谢你的帮助!
<?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'));