AWS Developer Tools Blog

Logging Requests

TheAWS 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 theaws-sdk gem, you need to configure a logger. The easiest way is to create aLogger (from Ruby’s standard lib) and pass it toAWS.config.

require 'aws-sdk'require 'logger'AWS.config(:logger => Logger.new($stdout))# make a requests3 = AWS::S3.news3.buckets['aws-sdk'].objects['key'].head# log output sent to standard outI, [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 withAWS.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 aAWS::Core::Response object and then return a formatted log message. The built-inAWS::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 outputI, [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 toAWS.config and start making requests.

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

Logging in Rails

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