httplog 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +37 -4
- data/lib/httplog/http_log.rb +24 -14
- data/lib/httplog/version.rb +1 -1
- metadata +3 -6
- data/lib/tasks/http_log_tasks.rake +0 -4
data/README.rdoc
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
== httplog
|
2
2
|
|
3
3
|
Log outgoing HTTP requests made from your application.
|
4
|
+
See the {blog post}[http://trusche.github.com/blog/2011/09/29/logging-outgoing-http-requests/]
|
5
|
+
for more details.
|
4
6
|
|
5
7
|
=== Installation
|
6
8
|
|
@@ -29,10 +31,40 @@ You can override the following default options:
|
|
29
31
|
|
30
32
|
So if you want to use this in a Rails app:
|
31
33
|
|
32
|
-
|
33
|
-
|
34
|
+
# config/initializers/httplog.rb
|
34
35
|
HttpLog.options[:logger] = Rails.logger
|
35
36
|
|
37
|
+
=== Example
|
38
|
+
|
39
|
+
With the default configuration, the log output might look like this:
|
40
|
+
|
41
|
+
D, [2012-11-21T15:09:03.532970 #6857] DEBUG -- : [httplog] Connecting: localhost
|
42
|
+
D, [2012-11-21T15:09:03.533877 #6857] DEBUG -- : [httplog] Sending: GET http://localhost:9292/index.html
|
43
|
+
D, [2012-11-21T15:09:03.534499 #6857] DEBUG -- : [httplog] Status: 200
|
44
|
+
D, [2012-11-21T15:09:03.534544 #6857] DEBUG -- : [httplog] Benchmark: 0.00057 seconds
|
45
|
+
D, [2012-11-21T15:09:03.534578 #6857] DEBUG -- : [httplog] Response:
|
46
|
+
<html>
|
47
|
+
<head>
|
48
|
+
<title>Test Page</title>
|
49
|
+
</head>
|
50
|
+
<body>
|
51
|
+
<h1>This is the test page.</h1>
|
52
|
+
</body>
|
53
|
+
</html>
|
54
|
+
|
55
|
+
|
56
|
+
=== Known Issues
|
57
|
+
|
58
|
+
When using open-uri, the reading of the HTTP response body is deferred,
|
59
|
+
so it is not available for logging. This will be noted in the logging statement:
|
60
|
+
|
61
|
+
D, [2012-11-21T15:09:03.547005 #6857] DEBUG -- : [httplog] Connecting: localhost
|
62
|
+
D, [2012-11-21T15:09:03.547938 #6857] DEBUG -- : [httplog] Sending: GET http://localhost:9292/index.html
|
63
|
+
D, [2012-11-21T15:09:03.548615 #6857] DEBUG -- : [httplog] Status: 200
|
64
|
+
D, [2012-11-21T15:09:03.548662 #6857] DEBUG -- : [httplog] Benchmark: 0.000617 seconds
|
65
|
+
D, [2012-11-21T15:09:03.548695 #6857] DEBUG -- : [httplog] Response: (not available yet)
|
66
|
+
|
67
|
+
|
36
68
|
=== Running the specs
|
37
69
|
|
38
70
|
Make sure you have the necessary dependencies installed by running `bundle install`.
|
@@ -43,5 +75,6 @@ This will launch a simple rack server on port 9292 and run all tests locally aga
|
|
43
75
|
|
44
76
|
If you have any issues with httplog,
|
45
77
|
or feature requests,
|
46
|
-
please
|
47
|
-
or fork the project and send a pull request.
|
78
|
+
please {add an issue}[https://github.com/trusche/httplog/issues] on GitHub
|
79
|
+
or fork the project and send a pull request.
|
80
|
+
Please include passing specs with all pull requests.
|
data/lib/httplog/http_log.rb
CHANGED
@@ -9,6 +9,7 @@ module HttpLog
|
|
9
9
|
:severity => Logger::Severity::DEBUG,
|
10
10
|
:log_connect => true,
|
11
11
|
:log_request => true,
|
12
|
+
:log_headers => false,
|
12
13
|
:log_data => true,
|
13
14
|
:log_status => true,
|
14
15
|
:log_response => true,
|
@@ -18,7 +19,7 @@ module HttpLog
|
|
18
19
|
end
|
19
20
|
|
20
21
|
def self.log(msg)
|
21
|
-
@@options[:logger].add(@@options[:severity]) { msg }
|
22
|
+
@@options[:logger].add(@@options[:severity]) { "[httplog] #{msg}" }
|
22
23
|
end
|
23
24
|
|
24
25
|
end
|
@@ -33,34 +34,43 @@ module Net
|
|
33
34
|
|
34
35
|
if started? && !HttpLog.options[:compact_log]
|
35
36
|
if HttpLog.options[:log_request]
|
36
|
-
HttpLog::log("
|
37
|
+
HttpLog::log("Sending: #{req.method} http://#{@address}:#{@port}#{req.path}")
|
37
38
|
end
|
38
39
|
|
39
40
|
if HttpLog.options[:log_headers]
|
40
41
|
req.each_header do |key,value|
|
41
|
-
HttpLog::log("
|
42
|
+
HttpLog::log("Header: #{key}: #{value}")
|
42
43
|
end
|
43
44
|
end
|
44
45
|
|
45
46
|
if req.method == "POST" && HttpLog.options[:log_data]
|
46
|
-
#
|
47
|
-
# in that case req.body will be empty
|
47
|
+
# A bit convoluted becase post_form uses form_data= to assign the data, so
|
48
|
+
# in that case req.body will be empty.
|
48
49
|
data = req.body.nil? || req.body.size == 0 ? body : req.body
|
49
|
-
HttpLog::log("
|
50
|
+
HttpLog::log("Data: #{data}")
|
50
51
|
end
|
51
52
|
end
|
52
53
|
|
53
|
-
|
54
|
-
response
|
55
|
-
benchmark = Time.now -
|
54
|
+
ts_start = Time.now
|
55
|
+
response = orig_request(req, body, &block)
|
56
|
+
benchmark = Time.now - ts_start
|
56
57
|
|
57
58
|
if started?
|
58
59
|
if HttpLog.options[:compact_log]
|
59
|
-
HttpLog::log("
|
60
|
+
HttpLog::log("#{req.method} http://#{@address}:#{@port}#{req.path} completed with status code #{response.code} in #{benchmark} seconds")
|
60
61
|
else
|
61
|
-
HttpLog::log("
|
62
|
-
HttpLog::log("
|
63
|
-
|
62
|
+
HttpLog::log("Status: #{response.code}") if HttpLog.options[:log_status]
|
63
|
+
HttpLog::log("Benchmark: #{benchmark} seconds") if HttpLog.options[:log_benchmark]
|
64
|
+
|
65
|
+
if HttpLog.options[:log_response]
|
66
|
+
if response.body.is_a?(Net::ReadAdapter)
|
67
|
+
# open-uri wraps the response in a Net::ReadAdapter that defers reading
|
68
|
+
# the contrent, so the reponse body is not available here.
|
69
|
+
HttpLog::log("Response: (not available yet)")
|
70
|
+
else
|
71
|
+
HttpLog::log("Response:\n#{response.body.to_s}")
|
72
|
+
end
|
73
|
+
end
|
64
74
|
end
|
65
75
|
end
|
66
76
|
|
@@ -69,7 +79,7 @@ module Net
|
|
69
79
|
|
70
80
|
def connect
|
71
81
|
unless started? || HttpLog.options[:compact_log]
|
72
|
-
HttpLog::log("
|
82
|
+
HttpLog::log("Connecting: #{@address}") if HttpLog.options[:log_connect]
|
73
83
|
end
|
74
84
|
orig_connect
|
75
85
|
end
|
data/lib/httplog/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: httplog
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-11-21 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -38,7 +38,6 @@ files:
|
|
38
38
|
- lib/httplog/http_log.rb
|
39
39
|
- lib/httplog/version.rb
|
40
40
|
- lib/httplog.rb
|
41
|
-
- lib/tasks/http_log_tasks.rake
|
42
41
|
- MIT-LICENSE
|
43
42
|
- Rakefile
|
44
43
|
- README.rdoc
|
@@ -54,9 +53,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
54
53
|
- - ! '>='
|
55
54
|
- !ruby/object:Gem::Version
|
56
55
|
version: '0'
|
57
|
-
segments:
|
58
|
-
- 0
|
59
|
-
hash: 418187136863777166
|
60
56
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
61
57
|
none: false
|
62
58
|
requirements:
|
@@ -70,3 +66,4 @@ signing_key:
|
|
70
66
|
specification_version: 3
|
71
67
|
summary: Logs outgoing Net::HTTP requests.
|
72
68
|
test_files: []
|
69
|
+
has_rdoc:
|