marcopolo 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +10 -2
- data/lib/marcopolo.rb +6 -1
- data/lib/marcopolo/middleware.rb +26 -12
- data/lib/marcopolo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: afc67f4a1453ec6bb4f6eee5c4416c72f8b9b9e1
|
4
|
+
data.tar.gz: f1f2349517951a120298d76dcf9e40b0db217161
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bee378b9d266328c4be7a3a196e0b44b3d3b31c5795caf1674bc1d2862188badfd90267954cf624ee8921a5f04447e71781d5847449c575960edbcfc231eef4c
|
7
|
+
data.tar.gz: 61b0b157dafd8cca699c0f49ded943f9087f464e53a9d1764201c793804ad9442062f3d4dddf3c2465f04758d2db0d3fa37be07d87f1b6663e10d32d0ae4db5f
|
data/README.md
CHANGED
@@ -21,14 +21,22 @@ Configure the logger settings in a Rails initializer like so:
|
|
21
21
|
Marcopolo.options[:logger] = Logger.new(File.join(Rails.root, 'log', "#{Rails.env}-http-raw.log"))
|
22
22
|
Marcopolo.options[:severity] = Logger::Severity::DEBUG
|
23
23
|
|
24
|
-
Consider adding
|
24
|
+
Consider adding logrotate rules to your application server to prevent these extremely verbose logs from blowing up your storage.
|
25
25
|
|
26
26
|
Also, be mindful of cleaning up these logs appropriately, as sensative information like authorization headers and cookies will not be filtered out automatically (hence "raw").
|
27
27
|
|
28
|
+
### Filtering out noise
|
29
|
+
|
30
|
+
You may want to filter out noisy requests, such as health checks. Create a proc that takes a [Rack::Request](http://rack.rubyforge.org/doc/classes/Rack/Request.html) object and returns `true` if the request is desirable and `false` if the request should be excluded from the log.
|
31
|
+
|
32
|
+
Marcopolo.options[:filter] = Proc.new do |request|
|
33
|
+
# exclude all options requests
|
34
|
+
request.request_method != 'OPTIONS']
|
35
|
+
end
|
36
|
+
|
28
37
|
## TODO
|
29
38
|
|
30
39
|
* Support for Sinatra?
|
31
|
-
* Noise filters?
|
32
40
|
* Ability to disable for certain endpoints, etc
|
33
41
|
* IP-based log segmenting?
|
34
42
|
* Tests?
|
data/lib/marcopolo.rb
CHANGED
@@ -6,7 +6,8 @@ module Marcopolo
|
|
6
6
|
DEFAULT_LOGGER = Logger.new($stdout)
|
7
7
|
DEFAULT_OPTIONS = {
|
8
8
|
logger: DEFAULT_LOGGER,
|
9
|
-
severity: Logger::Severity::DEBUG
|
9
|
+
severity: Logger::Severity::DEBUG,
|
10
|
+
filter: Proc.new {|request| true }
|
10
11
|
}
|
11
12
|
|
12
13
|
class << self
|
@@ -17,5 +18,9 @@ module Marcopolo
|
|
17
18
|
def log(msg)
|
18
19
|
options[:logger].add(options[:severity]) { msg }
|
19
20
|
end
|
21
|
+
|
22
|
+
def allow(request)
|
23
|
+
options[:filter].call(request)
|
24
|
+
end
|
20
25
|
end
|
21
26
|
end
|
data/lib/marcopolo/middleware.rb
CHANGED
@@ -5,45 +5,59 @@ module Marcopolo
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def call(env)
|
8
|
-
|
8
|
+
@request = Rack::Request.new(env)
|
9
9
|
|
10
|
+
@status, @headers, @response = @app.call(env)
|
11
|
+
|
12
|
+
if Marcopolo.allow(@request)
|
13
|
+
begin
|
14
|
+
rawlog(env)
|
15
|
+
rescue => e
|
16
|
+
Marcopolo.log "Failed to log request: #{e.message}"
|
17
|
+
end
|
18
|
+
else
|
19
|
+
Marcopolo.log "Filtering request: #{@request.request_method} #{@request.url}"
|
20
|
+
end
|
21
|
+
|
22
|
+
return [@status, @headers, @response]
|
23
|
+
end
|
24
|
+
|
25
|
+
def rawlog(env)
|
10
26
|
req_headers = env.select {|k,v| k.start_with? 'HTTP_'}
|
11
27
|
.collect {|pair| [pair[0].sub(/^HTTP_/, '').split('_').map(&:titleize).join('-'), pair[1]]}
|
12
28
|
.sort
|
13
29
|
|
14
30
|
req_hash = {
|
15
31
|
"REQUEST" => "",
|
16
|
-
"Remote Address" =>
|
17
|
-
"Request URL" =>
|
18
|
-
"Request Method" =>
|
32
|
+
"Remote Address" => @request.ip,
|
33
|
+
"Request URL" => @request.url,
|
34
|
+
"Request Method" => @request.request_method,
|
19
35
|
"REQUEST HEADERS" => ""
|
20
36
|
}
|
21
37
|
|
22
38
|
req_headers.to_a.each {|i| req_hash["\t" + i.first] = i.last }
|
23
39
|
|
24
40
|
req_hash.merge!({
|
25
|
-
"Request Body" =>
|
41
|
+
"Request Body" => @request.body.gets
|
26
42
|
})
|
27
43
|
|
28
44
|
Marcopolo.log req_hash.to_a.map {|o| o.join(': ') }.join("\n") + "\n"
|
29
45
|
|
30
|
-
status, headers, response = @app.call(env)
|
31
|
-
|
32
46
|
resp_hash = {
|
33
47
|
"RESPONSE" => "",
|
34
|
-
"Response Status" =>
|
48
|
+
"Response Status" => @status,
|
35
49
|
"Response Headers" => ""
|
36
50
|
}
|
37
51
|
|
38
|
-
|
52
|
+
@headers.to_a.each {|i| resp_hash["\t" + i.first] = i.last }
|
53
|
+
|
54
|
+
response_body = @response.respond_to?(:body) ? @response.body : @response
|
39
55
|
|
40
56
|
resp_hash.merge!({
|
41
|
-
"Response Body" =>
|
57
|
+
"Response Body" => response_body
|
42
58
|
})
|
43
59
|
|
44
60
|
Marcopolo.log resp_hash.to_a.map {|o| o.join(': ') }.join("\n") + "\n"
|
45
|
-
|
46
|
-
return [status, headers, response]
|
47
61
|
end
|
48
62
|
end
|
49
63
|
end
|
data/lib/marcopolo/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: marcopolo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andrew Hammond
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-05-
|
11
|
+
date: 2014-05-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|