class Aws::Log::Formatter

A log formatter generates a string for logging from a response. This accomplished with a log pattern string:

pattern = ':operation :http_response_status_code :time'
formatter = Aws::Log::Formatter.new(pattern)
formatter.format(response)
#=> 'get_bucket 200 0.0352'

# Canned Formatters

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

# Pattern Substitutions

You can put any of these placeholders into you pattern.

* `:region` - The region configured for the client.

* `:client_class` - The name of the client class.

* `:operation` - The name of the client request method.

* `:request_params` - The user provided request parameters. Long
  strings are truncated/summarized if they exceed the
  `:max_string_size`.  Other objects are inspected.

* `:time` - The total time in seconds spent on the
  request.  This includes client side time spent building
  the request and parsing the response.

* `:retries` - The number of times a client request was retried.

* `:http_request_method` - The http request verb, e.g., `POST`,
  `PUT`, `GET`, etc.

* `:http_request_endpoint` - The request endpoint.  This includes
   the scheme, host and port, but not the path.

* `:http_request_scheme` - 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_headers` - The http request headers, inspected.

* `:http_request_body` - The http request payload.

* `:http_response_status_code` - 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.

* `:error_class`

* `:error_message`

Attributes

pattern[R]

@return [String]

Public Class Methods

colored(options = {}) click to toggle source

The default log format with ANSI colors. @option (see initialize) @example A sample of the colored format (sans the ansi colors).

[ClientClass 200 0.580066 0 retries] list_objects(:bucket_name => 'bucket')

@return [Formatter]

# File lib/aws-sdk-core/log/formatter.rb, line 249
def colored(options = {})
  bold = "\x1b[1m"
  color = "\x1b[34m"
  reset = "\x1b[0m"
  pattern = []
  pattern << "#{bold}#{color}[:client_class"
  pattern << ":http_response_status_code"
  pattern << ":time"
  pattern << ":retries retries]#{reset}#{bold}"
  pattern << ":operation(:request_params)"
  pattern << ":error_class"
  pattern << ":error_message#{reset}"
  Formatter.new(pattern.join(' ') + "\n", options)
end
default(options = {}) click to toggle source

The default log format. @option (see initialize) @example A sample of the default format.

[ClientClass 200 0.580066 0 retries] list_objects(:bucket_name => 'bucket')

@return [Formatter]

# File lib/aws-sdk-core/log/formatter.rb, line 212
def default(options = {})
  pattern = []
  pattern << "[:client_class"
  pattern << ":http_response_status_code"
  pattern << ":time"
  pattern << ":retries retries]"
  pattern << ":operation(:request_params)"
  pattern << ":error_class"
  pattern << ":error_message"
  Formatter.new(pattern.join(' ') + "\n", options)
end
new(pattern, options = {}) click to toggle source

@param [String] pattern The log format pattern should be a string

and may contain substitutions.

@option options [Integer] :max_string_size (1000) When summarizing

request parameters, strings longer than this value will be
truncated.

@option options [Array<Symbol>] :filter A list of parameter

names that should be filtered when logging `:request_params`.

    Formatter.new(pattern, filter: [:password])

The default list of filtered parameters is documented on the
{ParamFilter} class.

@option options [Boolean] :filter_sensitive_params (true) Set to false

to disable the sensitive parameter filtering when logging
`:request_params`.
# File lib/aws-sdk-core/log/formatter.rb, line 93
def initialize(pattern, options = {})
  @pattern = pattern
  @param_formatter = ParamFormatter.new(options)
  @param_filter = ParamFilter.new(options)
end
short(options = {}) click to toggle source

The short log format. Similar to default, but it does not inspect the request params or report on retries. @option (see initialize) @example A sample of the short format

[ClientClass 200 0.494532] list_buckets

@return [Formatter]

# File lib/aws-sdk-core/log/formatter.rb, line 232
def short(options = {})
  pattern = []
  pattern << "[:client_class"
  pattern << ":http_response_status_code"
  pattern << ":time]"
  pattern << ":operation"
  pattern << ":error_class"
  Formatter.new(pattern.join(' ') + "\n", options)
end

Public Instance Methods

format(response) click to toggle source

Given a response, this will format a log message and return it as a

string according to {#pattern}.

@param [Seahorse::Client::Response] response @return [String]

# File lib/aws-sdk-core/log/formatter.rb, line 106
def format(response)
  pattern.gsub(/:(\w+)/) { |sym| send("_#{sym[1..-1]}", response) }
end
method_missing(method_name, *args) click to toggle source

@api private

Calls superclass method
# File lib/aws-sdk-core/log/formatter.rb, line 111
def method_missing(method_name, *args)
  if method_name.to_s.chars.first == '_'
    ":#{method_name.to_s[1..-1]}"
  else
    super
  end
end

Private Instance Methods

_client_class(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 125
def _client_class(response)
  response.context.client.class.name
end
_error_class(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 195
def _error_class(response)
  response.error ? response.error.class.name : ''
end
_error_message(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 199
def _error_message(response)
  response.error ? response.error.message : ''
end
_http_request_body(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 173
def _http_request_body(response)
  @param_formatter.summarize(response.context.http_request.body_contents)
end
_http_request_endpoint(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 149
def _http_request_endpoint(response)
  response.context.http_request.endpoint.to_s
end
_http_request_headers(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 169
def _http_request_headers(response)
  response.context.http_request.headers.inspect
end
_http_request_host(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 157
def _http_request_host(response)
  response.context.http_request.endpoint.host
end
_http_request_method(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 165
def _http_request_method(response)
  response.context.http_request.http_method
end
_http_request_port(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 161
def _http_request_port(response)
  response.context.http_request.endpoint.port.to_s
end
_http_request_scheme(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 153
def _http_request_scheme(response)
  response.context.http_request.endpoint.scheme
end
_http_response_body(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 185
def _http_response_body(response)
  if response.context.http_response.body.respond_to?(:rewind)
    @param_formatter.summarize(
      response.context.http_response.body_contents
    )
  else
    ''
  end
end
_http_response_headers(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 181
def _http_response_headers(response)
  response.context.http_response.headers.inspect
end
_http_response_status_code(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 177
def _http_response_status_code(response)
  response.context.http_response.status_code.to_s
end
_operation(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 129
def _operation(response)
  response.context.operation_name
end
_region(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 121
def _region(response)
  response.context.config.region
end
_request_params(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 133
def _request_params(response)
  params = response.context.params
  type = response.context.operation.input.shape.struct_class
  @param_formatter.summarize(@param_filter.filter(params, type))
end
_retries(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 145
def _retries(response)
  response.context.retries
end
_time(response) click to toggle source
# File lib/aws-sdk-core/log/formatter.rb, line 139
def _time(response)
  duration = response.context[:logging_completed_at] -
    response.context[:logging_started_at]
  ("%.06f" % duration).sub(/0+$/, '')
end