Class: AWS::Core::LogFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/aws/core/log_formatter.rb

Overview

Log Formatters

Log formatters receive a Response object and return a log message. When you construct a LogFormatter, you provide a pattern string with substitutions.

pattern = '[REQUEST :http_status_code] :service :operation :duration'
formatter = AWS::Core::LogFormatter.new(pattern)
formatter.format(response)
#=> '[AWS 200] EC2 get_bucket 0.0352'

AWS Configuration

AWS.config provides a LogFormatter.default log formatter. You can repace this formatter by building your own and then passing it to AWS.config.

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

Canned Formatters

Instead of providing your own pattern, you can choose a canned log formatter.

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

Here is the list of canned formatters.

Pattern Substitutions

You can put any of these placeholders into you pattern.

:service

The AWS service name (e.g. ‘S3’, ‘EC2’, ‘SimpleDB’, etc)

:region

The AWS region name (e.g. ‘us-east-1’, ‘us-west-1’, etc)

:operation

The name of the client request method. This maps to the name of the serivce API operation (e.g. :describe_instances).

:options

The hash of options passed to the client request method. Long strings are truncated/summarized if they excede the log formatters #max_string_size. Other objects are inspected.

:retry_count

The number of times a client request was retried. Throttlings and service errors trigger the automatic retry logic. This value indicates how many extra attempts were made before getting a successful response or giving up.

:duration

The time it took to generate a response, expressed in decimal seconds. This time includes everything from calling the client request method, until that method returns a value (event retries and retry delays).

:error_class

The class name of the error returned by the service. If no error was returned, this will be replcaed by an empty string.

:error_message

The message of the error returned. If no error was returned by the service, this will be an empty string.

:http_request_method

The HTTP request verb (e.g. ‘POST’, ‘PUT’, ‘GET’, etc).

:http_request_protocol

This is replaced by ‘http’ or ‘https’.

:http_request_host

The host name of the http request endpoint (e.g. ‘s3.amazon.com’).

:http_request_port

The port number (e.g. ‘443’ or ‘80’).

:http_request_uri

The http request uri folling the host (e.g. ‘/bucket_name/objects/key?versions’).

:http_request_body

The http request payload.

:http_request_headers

The http request headers, inspected.

:http_request_proxy_uri

The proxy uri used, or an empty string.

:http_response_status

The http response status code (e.g. ‘200’, ‘404’, ‘500’, etc).

:http_response_headers

The http response headers, inspected.

:http_response_body

The http response body contents.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pattern, options = {}) ⇒ LogFormatter

Returns a new instance of LogFormatter.

Parameters:

  • pattern (String)

    The log format pattern should be a string and may contain any of the following placeholders:

    • :service

    • :region

    • :operation

    • :options

    • +:retry_count

    • :duration

    • :error_class

    • :error_message

    • :http_request_method

    • :http_request_protocol

    • :http_request_host

    • :http_request_port

    • :http_request_uri

    • :http_request_body

    • :http_request_headers

    • :http_request_proxy_uri

    • :http_response_status

    • :http_response_headers

    • :http_response_body

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :max_string_size (Integer) — default: 1000


157
158
159
160
# File 'lib/aws/core/log_formatter.rb', line 157

def initialize pattern, options = {}
  @pattern = pattern
  @max_string_size = options[:max_string_size] || 1000
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object (protected)



182
183
184
185
186
187
188
# File 'lib/aws/core/log_formatter.rb', line 182

def method_missing method_name, *args
  if method_name.to_s.chars.first == '_'
    ":#{method_name.to_s[1..-1]}"
  else
    super
  end
end

Instance Attribute Details

#max_string_sizeInteger (readonly)

Returns:

  • (Integer)


166
167
168
# File 'lib/aws/core/log_formatter.rb', line 166

def max_string_size
  @max_string_size
end

#patternString (readonly)

Returns:

  • (String)


163
164
165
# File 'lib/aws/core/log_formatter.rb', line 163

def pattern
  @pattern
end

Class Method Details

.coloredLogFormatter

The default log format with ANSI colors.

Examples:

A sample of the colored format (sans the ansi colors).


[AWS SimpleEmailService 200 0.580066 0 retries] list_verified_email_addresses()

Returns:



438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
# File 'lib/aws/core/log_formatter.rb', line 438

def colored

  bold = "\x1b[1m"
  color = "\x1b[34m"
  reset = "\x1b[0m"

  pattern = []
  pattern << "#{bold}#{color}[AWS"
  pattern << ":service"
  pattern << ":http_response_status"
  pattern << ":duration"
  pattern << ":retry_count retries]#{reset}#{bold}"
  pattern << ":operation(:options)"
  pattern << ":error_class"
  pattern << ":error_message#{reset}"

  LogFormatter.new(pattern.join(' ') + "\n")

end

.debugLogFormatter

A debug format that dumps most of the http request and response data.

Examples:

A truncated sample of the debug format.


+-------------------------------------------------------------------------------
| AWS us-east-1 SimpleEmailService list_verified_email_addresses 0.429189 0 retries
+-------------------------------------------------------------------------------
|   REQUEST
+-------------------------------------------------------------------------------
|    METHOD: POST
|       URL: https://email.us-east-1.amazonaws.com::443:/
|   HEADERS: {"content-type"=>"application/x-www-form-urlencoded" ...
|      BODY: Action=ListVerifiedEmailAddresses&Timestamp= ...
+-------------------------------------------------------------------------------
|  RESPONSE
+-------------------------------------------------------------------------------
|    STATUS: 200
|   HEADERS: {"x-amzn-requestid"=>["..."], ...
|      BODY: <ListVerifiedEmailAddressesResponse ...

Returns:



389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/aws/core/log_formatter.rb', line 389

def debug

  sig_pattern = []
  sig_pattern << ':region'
  sig_pattern << ':service'
  sig_pattern << ':operation'
  sig_pattern << ':duration'
  sig_pattern << ':retry_count retries'

  uri_pattern = []
  uri_pattern << ':http_request_protocol'
  uri_pattern << '://'
  uri_pattern << ':http_request_host'
  uri_pattern << '::'
  uri_pattern << ':http_request_port'
  uri_pattern << ':'
  uri_pattern << ':http_request_uri'

  line = "+" + '-' * 79

  pattern = []
  pattern << line
  pattern << "| AWS #{sig_pattern.join(' ')}"
  pattern << line
  pattern << "|   REQUEST"
  pattern << line
  pattern << "|    METHOD: :http_request_method"
  pattern << "|       URL: #{uri_pattern.join}"
  pattern << "|   HEADERS: :http_request_headers"
  pattern << "|      BODY: :http_request_body"
  pattern << line
  pattern << "|  RESPONSE"
  pattern << line
  pattern << "|    STATUS: :http_response_status"
  pattern << "|   HEADERS: :http_response_headers"
  pattern << "|      BODY: :http_response_body"

  LogFormatter.new(pattern.join("\n") + "\n")

end

.defaultLogFormatter

The default log format.

Examples:

A sample of the default format.


[AWS SimpleEmailService 200 0.580066 0 retries] list_verified_email_addresses()

Returns:



327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# File 'lib/aws/core/log_formatter.rb', line 327

def default

  pattern = []
  pattern << "[AWS"
  pattern << ":service"
  pattern << ":http_response_status"
  pattern << ":duration"
  pattern << ":retry_count retries]"
  pattern << ":operation(:options)"
  pattern << ":error_class"
  pattern << ":error_message"

  LogFormatter.new(pattern.join(' ') + "\n")

end

.shortLogFormatter

The short log format. Similar to default, but it does not inspect the request params or report on retries.

Examples:

A sample of the short format


[AWS SimpleEmailService 200 0.494532] list_verified_email_addresses

Returns:



352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/aws/core/log_formatter.rb', line 352

def short

  pattern = []
  pattern << "[AWS"
  pattern << ":service"
  pattern << ":http_response_status"
  pattern << ":duration]"
  pattern << ":operation"
  pattern << ":error_class"

  LogFormatter.new(pattern.join(' ') + "\n")

end

Instance Method Details

#format(response) ⇒ String

Parameters:

Returns:

  • (String)


170
171
172
# File 'lib/aws/core/log_formatter.rb', line 170

def format response
  pattern.gsub(/:(\w+)/) {|sym| send("_#{sym[1..-1]}", response) }
end