Skip to content

Commit

Permalink
Pull request and response logging into methods
Browse files Browse the repository at this point in the history
  • Loading branch information
drbrain committed Apr 8, 2011
1 parent 3c0e22b commit 555548c
Showing 1 changed file with 24 additions and 11 deletions.
35 changes: 24 additions & 11 deletions lib/mechanize.rb
Expand Up @@ -568,6 +568,17 @@ def request_language_charset request
request['accept-language'] = 'en-us,en;q=0.5'
end

# Log specified headers for the request
def request_log request
return unless log

log.info("#{request.class}: #{request.path}")

request.each_header do |k, v|
log.debug("request-header: #{k} => #{v}")
end
end

def request_add_headers request, headers = {}
@request_headers.each do |k,v|
request[k] = v
Expand Down Expand Up @@ -658,6 +669,17 @@ def response_follow_meta_refresh response, uri, page, redirects
end
end

def response_log response
return unless log

log.info("status: #{res.class} #{res.http_version} #{res.code} " \
"#{res.message}")

res.each_header do |k, v|
log.debug("response-header: #{k} => #{v}")
end
end

def response_parse response, body, uri
content_type = nil

Expand Down Expand Up @@ -831,22 +853,13 @@ def fetch_page uri, method = :get, headers = {}, params = [],
connection.open_timeout = @open_timeout if @open_timeout
connection.read_timeout = @read_timeout if @read_timeout

# Log specified headers for the request
log.info("#{request.class}: #{request.path}") if log
request.each_header do |k, v|
log.debug("request-header: #{ k } => #{ v }")
end if log
request_log request

response_body = nil

# Send the request
response = connection.request(uri, request) { |res|
log.info("status: #{res.class} #{res.http_version} #{res.code} " \
"#{res.message}") if log

res.each_header do |k, v|
log.debug("response-header: #{ k } => #{ v }")
end if log
response_log res

response_body = response_read res, request

Expand Down

0 comments on commit 555548c

Please sign in to comment.