Merge pull request #80 from relaxdiego/fix/79

Improve logging
This commit is contained in:
Mark Maglana 2013-11-05 15:02:59 -08:00
commit aedb746a6c
5 changed files with 66 additions and 25 deletions

View File

@ -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"

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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