Tag: Logging

Wire Logging in the AWS SDK for PHP

by Jeremy Lindblom | on | in PHP | Permalink | Comments |  Share

One of the features of the AWS SDK for PHP that I often recommend to customers is the LogPlugin, that can be used to do wire logging. It is one of the many plugins included with Guzzle, which is the underlying HTTP library used by the SDK. Guzzle’s LogPlugin includes a default configuration that will output the content of the requests and responses sent over the wire to AWS. You can use it to help debug requests or just learn more about how the AWS APIs work.

Adding the LogPlugin to any client in the SDK is simple. The following shows how to set it up.

$logPlugin = GuzzlePluginLogLogPlugin::getDebugPlugin();

The output generated by LogPlugin for a single request looks similar to the following text (this request was for executing an Amazon S3 ListBuckets operation).

# Request:
GET / HTTP/1.1
Host: s3.amazonaws.com
User-Agent: aws-sdk-php2/2.4.6 Guzzle/3.7.3 curl/7.25.0 PHP/5.3.27
Date: Fri, 27 Sep 2013 15:53:10 +0000

# Response:
HTTP/1.1 200 OK
x-amz-id-2: EXAMPLE4j/v8onDxyeuFaQFsNvN66EXAMPLE30KQLfq0T6sVcLxj
x-amz-request-id: 4F3EXAMPLEE14
Date: Fri, 27 Sep 2013 15:53:09 GMT
Content-Type: application/xml
Transfer-Encoding: chunked
Server: AmazonS3

<?xml version="1.0" encoding="UTF-8"?>
<ListAllMyBucketsResult xmlns="http://s3.amazonaws.com/doc/2006-03-01/">[...]</ListAllMyBucketsResult>

This is the output generated using the default configuration. You can configure the LogPlugin to customize the behavior, format, and location of what is logged. It’s also possible to integrate with third-party logging libraries like Monolog. For more information, see the section about the wire logger in the AWS SDK for PHP User Guide.

Logging HTTP Wire Traces

by Trevor Rowe | on | in Ruby | Permalink | Comments |  Share

In a previous previous blog post, I wrote about how to log requests generated by the AWS SDK for Ruby (aws-sdk gem). While this can be a valuable tool in seeing how your code translates into requests to AWS, it doesn’t do everything. What if you think the SDK is serializing your request incorrectly? Sometimes anything short of a HTTP wire trace just isn’t enough.

That problem is easily solved:

ddb = AWS::DynamoDB.new(:http_wire_trace => true)
#=> 'aws-sdk-test'

This will send the following to your configured logger:

opening connection to dynamodb.us-east-1.amazonaws.com...
<- "POST / HTTP/1.1rnContent-Type: application/x-amz-json-1.0rnX-Amz-Target: DynamoDB_20111205.ListTablesrnContent-Length: 11rnUser-Agent: aws-sdk-ruby/1.8.5 ruby/1.9.3 x86_64-darwin11.4.2rnHost: dynamodb.us-east-1.amazonaws.comrnX-Amz-Date: 20130315T163624ZrnX-Amz-Content-Sha256: 55522f708dcfebccb7bd3e8d0001a53ecaf2beca9ca801f1e9161e24215faa99rnAuthorization: AWS4-HMAC-SHA256 Credential=AKIAJUNH63P3WCTAYHFA/20130315/us-east-1/dynamodb/aws4_request, SignedHeaders=content-length;content-type;host;user-agent;x-amz-content-sha256;x-amz-date;x-amz-target, Signature=8a5a5082afb33542eacd3c9429a5efaa80194b6e8390d4592da53f117d1d6d0drnAccept: */*rnrn"
<- "{"Limit":1}"
-> "HTTP/1.1 200 OKrn"
-> "x-amz-crc32: 349808839rn"
-> "Content-Type: application/x-amz-json-1.0rn"
-> "Content-Length: 71rn"
-> "Date: Fri, 15 Mar 2013 16:36:25 GMTrn"
-> "rn"
reading 71 bytes...
-> "{"LastEvaluatedTableName":"aws-sdk-test","TableNames":["aws-sdk-test"]}"
read 71 bytes
Conn keep-alive

If you have not configured a logger, this output will be sent to $stdout (very helpful when you are using IRB). You can also enable HTTP wire logging globally:

AWS.config(:logger => Logger.new($stderr), :http_wire_trace => true)


Logging Requests

by Trevor Rowe | on | in Ruby | Permalink | Comments |  Share

The AWS SDK for Ruby (aws-sdk gem) has some pretty cool logging features. I find them particularly helpful when I need to debug something. I generally jump into an IRB session that has a logger pre-wired for me and then start sending requests.

Configuring a Logger

To get log messages from the aws-sdk gem, you need to configure a logger. The easiest way is to create a Logger (from Ruby’s standard lib) and pass it to AWS.config.

require 'aws-sdk'
require 'logger'

AWS.config(:logger => Logger.new($stdout))

# make a request
s3 = AWS::S3.new

# log output sent to standard out
I, [2013-02-14T09:49:12.856086 #31922]  INFO -- : [AWS S3 200 0.194491 0 retries] head_object(:bucket_name=>"aws-sdk",:key=>"key")

By default, requests are logged with a level of :info. You can override the default log level with AWS.config.

AWS.config(:log_level => :debug)

Log Formatters

The default log message contain the following information:

  • The service class name (e.g. ‘S3’)
  • The HTTP response status code (e.g. 200)
  • The total time taken in seconds
  • The number of retries
  • A summary of the client method called

Similar to how you can configure :logger and :log_level, you can register a custom log formatter via :log_formatter. Log formatters accept a AWS::Core::Response object and then return a formatted log message. The built-in AWS::Core::LogFormatter class has support for simple pattern replacements.

pattern = '[REQUEST :http_status_code] :service :operation :duration'
formatter = AWS::Core::LogFormatter.new(pattern)

AWS::S3(:log_formatter => formatter).new.buckets.first

# log output
I, [2013-02-14T09:49:12.856086 #31922]  INFO -- : [REQUEST :http_status_code] S3 list_buckets 0.542574

Canned Log Formatters

You can choose from a handful of ready-to-use log formatters you can choose from, including:

  • AWS::Core::LogFormatter.default
  • AWS::Core::LogFormatter.short
  • AWS::Core::LogFormatter.debug
  • AWS::Core::LogFormatter.colored

Just pass one of these to AWS.config and start making requests.

AWS.config(:log_formatter => AWS::Core::LogFormatter.colored)

Logging in Rails

If you require the aws-sdk gem inside a Rails application, then the Ruby SDK automatically wire itself up to the Rails.logger. You are free to still configure a different logger or to change the log level or formatter.