http_logger 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/http_logger.gemspec +2 -2
- data/lib/http_logger.rb +48 -20
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.3.
|
1
|
+
0.3.2
|
data/http_logger.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{http_logger}
|
8
|
-
s.version = "0.3.
|
8
|
+
s.version = "0.3.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Bogdan Gusiev"]
|
12
|
-
s.date = %q{2013-02-
|
12
|
+
s.date = %q{2013-02-13}
|
13
13
|
s.description = %q{This gem keep an eye on every Net::HTTP library usage and dump all request and response data to the log file}
|
14
14
|
s.email = %q{agresso@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
data/lib/http_logger.rb
CHANGED
@@ -45,34 +45,62 @@ class HttpLogger
|
|
45
45
|
end
|
46
46
|
|
47
47
|
def perform(http, request, request_body)
|
48
|
-
|
48
|
+
start_time = Time.now
|
49
49
|
response = yield
|
50
50
|
ensure
|
51
|
-
if
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
request.each_capitalized { |k,v| log("HTTP request header", "#{k}: #{v}") } if self.class.log_headers
|
56
|
-
if request.is_a?(::Net::HTTP::Post) || request.is_a?(::Net::HTTP::Put)
|
57
|
-
log("#{request.class.to_s.upcase} params", request.body)
|
58
|
-
end
|
51
|
+
if require_logging?(http, request)
|
52
|
+
log_request_url(http, request, start_time)
|
53
|
+
log_post_put_params(request)
|
54
|
+
log_request_headers(request)
|
59
55
|
if defined?(response) && response
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
unless body.is_a?(Net::ReadAdapter)
|
64
|
-
if collapse_body_limit && collapse_body_limit > 0 && body.size >= collapse_body_limit
|
65
|
-
body = body[0..1000] + "\n\n<some data truncated>\n\n" + body[(body.size - 1000)..body.size]
|
66
|
-
end
|
67
|
-
log("Response body", body)
|
68
|
-
else
|
69
|
-
log("response body", "<impossible to log>")
|
70
|
-
end
|
56
|
+
log_response_code(response)
|
57
|
+
log_response_headers(response)
|
58
|
+
log_response_body(response.body)
|
71
59
|
end
|
72
60
|
end
|
73
61
|
end
|
74
62
|
|
75
63
|
protected
|
64
|
+
|
65
|
+
def log_request_url(http, request, start_time)
|
66
|
+
url = "http#{"s" if http.use_ssl?}://#{http.address}:#{http.port}#{request.path}"
|
67
|
+
ofset = Time.now - start_time
|
68
|
+
log("HTTP #{request.method} (%0.2fms)" % (ofset * 1000), URI.decode(url))
|
69
|
+
end
|
70
|
+
|
71
|
+
def log_request_headers(request)
|
72
|
+
if self.class.log_headers
|
73
|
+
request.each_capitalized { |k,v| log("HTTP request header", "#{k}: #{v}") }
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def log_post_put_params(request)
|
78
|
+
if request.is_a?(::Net::HTTP::Post) || request.is_a?(::Net::HTTP::Put)
|
79
|
+
log("#{request.class.to_s.upcase} params", request.body)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def log_response_code(response)
|
84
|
+
log("Response status", "#{response.class} (#{response.code})")
|
85
|
+
end
|
86
|
+
|
87
|
+
def log_response_headers(response)
|
88
|
+
if self.class.log_headers
|
89
|
+
response.each_capitalized { |k,v| log("HTTP response header", "#{k}: #{v}") }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def log_response_body(body)
|
94
|
+
if body && !body.is_a?(Net::ReadAdapter)
|
95
|
+
if collapse_body_limit && collapse_body_limit > 0 && body.size >= collapse_body_limit
|
96
|
+
body = body[0..1000] + "\n\n<some data truncated>\n\n" + body[(body.size - 1000)..body.size]
|
97
|
+
end
|
98
|
+
log("Response body", body)
|
99
|
+
else
|
100
|
+
log("Response body", "<impossible to log>")
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
76
104
|
def require_logging?(http, request)
|
77
105
|
fakeweb = if defined?(::FakeWeb)
|
78
106
|
uri = ::FakeWeb::Utility.request_uri_as_string(http, request)
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: http_logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 3
|
9
|
-
-
|
10
|
-
version: 0.3.
|
9
|
+
- 2
|
10
|
+
version: 0.3.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Bogdan Gusiev
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2013-02-
|
18
|
+
date: 2013-02-13 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|