1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
|
# frozen_string_literal: true
module Aws
module Plugins
# @see Log::Formatter
# @api private
class Logging < Seahorse::Client::Plugin
option(:logger,
doc_type: 'Logger',
docstring: <<-DOCS
The Logger instance to send log messages to. If this option
is not set, logging will be disabled.
DOCS
)
option(:log_level,
default: :info,
doc_type: Symbol,
docstring: 'The log level to send messages to the `:logger` at.'
)
option(:log_formatter,
doc_type: 'Aws::Log::Formatter',
doc_default: literal('Aws::Log::Formatter.default'),
docstring: 'The log formatter.'
) do |config|
Log::Formatter.default if config.logger
end
def add_handlers(handlers, config)
handlers.add(Handler, step: :validate) if config.logger
end
class Handler < Seahorse::Client::Handler
# @param [RequestContext] context
# @return [Response]
def call(context)
context[:logging_started_at] = Time.now
@handler.call(context).tap do |response|
context[:logging_completed_at] = Time.now
log(context.config, response)
end
end
private
# @param [Configuration] config
# @param [Response] response
# @return [void]
def log(config, response)
config.logger.send(config.log_level, format(config, response))
end
# @param [Configuration] config
# @param [Response] response
# @return [String]
def format(config, response)
config.log_formatter.format(response)
end
end
end
end
end
|