Improve logging
Logging was very sparse and needed more info regarding request and response body to be more useful. This change adds that capability.
This commit is contained in:
parent
8996c6cc8b
commit
974c87ab4b
|
@ -10,3 +10,4 @@ require "aviator/core/request_builder"
|
|||
require "aviator/core/response"
|
||||
require "aviator/core/service"
|
||||
require "aviator/core/session"
|
||||
require "aviator/core/logger"
|
||||
|
|
|
@ -0,0 +1,55 @@
|
|||
module Aviator
|
||||
|
||||
class Logger < Faraday::Response::Middleware
|
||||
extend Forwardable
|
||||
|
||||
def initialize(app, logger=nil)
|
||||
super(app)
|
||||
@logger = logger || begin
|
||||
require 'logger'
|
||||
::Logger.new(self.class::LOG_FILE_PATH)
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
def_delegators :@logger, :debug, :info, :warn, :error, :fatal
|
||||
|
||||
|
||||
def call(env)
|
||||
info(env[:method].to_s.upcase) { env[:url].to_s }
|
||||
debug('REQ_HEAD') { dump_headers env[:request_headers] }
|
||||
debug('REQ_BODY') { dump_body env[:body] }
|
||||
super
|
||||
end
|
||||
|
||||
|
||||
def on_complete(env)
|
||||
info('STATUS') { env[:status].to_s }
|
||||
debug('RES_HEAD') { dump_headers env[:response_headers] }
|
||||
debug('RES_BODY') { dump_body env[:body] }
|
||||
end
|
||||
|
||||
|
||||
def self.configure(log_file_path)
|
||||
# Return a subclass with its logfile path set. This
|
||||
# must be done so that different sessions can log to
|
||||
# different paths.
|
||||
Class.new(self) { const_set('LOG_FILE_PATH', log_file_path) }
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
def dump_body(body)
|
||||
return if body.nil?
|
||||
|
||||
# TODO: Make this configurable
|
||||
body.gsub(/(?<=["']password["']:["'])\w*(?=["'])/, '[FILTERED_VALUE]')
|
||||
end
|
||||
|
||||
def dump_headers(headers)
|
||||
headers.map { |k, v| "#{k}: #{v.inspect}" }.join("; ")
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -50,19 +50,6 @@ module Aviator
|
|||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
class Logger < Faraday::Response::Logger
|
||||
def initialize(app, logger=nil)
|
||||
super(app)
|
||||
@logger = logger || begin
|
||||
require 'logger'
|
||||
::Logger.new(self.class::LOG_FILE_PATH)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
attr_accessor :default_session_data
|
||||
|
||||
attr_reader :service,
|
||||
|
@ -84,7 +71,7 @@ module Aviator
|
|||
session_data = options[:session_data] || default_session_data
|
||||
|
||||
raise SessionDataNotProvidedError.new unless session_data
|
||||
|
||||
|
||||
[:base_url].each do |k|
|
||||
session_data[k] = options[k] if options[k]
|
||||
end
|
||||
|
@ -116,14 +103,9 @@ module Aviator
|
|||
|
||||
def http_connection
|
||||
@http_connection ||= Faraday.new do |conn|
|
||||
if log_file
|
||||
# Ugly hack to make logger configurable
|
||||
const_name = 'LOG_FILE_PATH'
|
||||
Logger.send(:remove_const, const_name) if Logger.const_defined?(const_name)
|
||||
Logger.const_set(const_name, log_file)
|
||||
conn.use Logger.dup
|
||||
end
|
||||
conn.use Logger.configure(log_file) if log_file
|
||||
conn.adapter Faraday.default_adapter
|
||||
|
||||
conn.headers['Content-Type'] = 'application/json'
|
||||
end
|
||||
end
|
||||
|
|
|
@ -33,7 +33,8 @@ class Aviator::Test
|
|||
unless @session
|
||||
@session = Aviator::Session.new(
|
||||
config_file: Environment.path,
|
||||
environment: 'openstack_admin'
|
||||
environment: 'openstack_admin',
|
||||
log_file: Environment.log_file_path
|
||||
)
|
||||
@session.authenticate
|
||||
end
|
||||
|
|
|
@ -8,11 +8,13 @@ class Test
|
|||
class << self
|
||||
|
||||
attr_reader :config,
|
||||
:log_file_path,
|
||||
:path
|
||||
|
||||
def init!
|
||||
@path = Pathname.new(__FILE__).join('..', '..', 'environment.yml').expand_path
|
||||
|
||||
@path = Pathname.new(__FILE__).join('..', '..', 'environment.yml').expand_path
|
||||
@log_file_path = Pathname.new(__FILE__).join('..', '..', '..', 'tmp', 'aviator.log').expand_path
|
||||
|
||||
unless path.file?
|
||||
raise <<-EOF
|
||||
|
||||
|
@ -30,7 +32,7 @@ copying the contents of environment.yml.example.
|
|||
|
||||
EOF
|
||||
end
|
||||
|
||||
|
||||
@config = YAML.load_file(path).with_indifferent_access
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue