rack-traffic-logger 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -1
- data/lib/rack/traffic_logger/echo.rb +1 -1
- data/lib/rack/traffic_logger/header_hash.rb +25 -0
- data/lib/rack/traffic_logger/version.rb +1 -1
- data/lib/rack/traffic_logger.rb +27 -18
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f26f03dcdb7e324d22fe2d1eef5c171d2b0a2a7e
|
4
|
+
data.tar.gz: 4dc100276db42b05d2d57673ec9a633de4ecef8a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b03a20d01b1c64ff07d84c450c21e4ecdba97626c0c892cc6c1a331a569c4ab10e64cb3a2e9e47f68c2e408b3c828fb5f01ed0e428670c69a49c1486fa721663
|
7
|
+
data.tar.gz: 6281b3c7a221db00a86e1d4eb6178b75fc53bac6e506922a742580cb823922f06dc7ab04f8a3a35593ff757091d05258f08b0150a10d42bb301c3a916606bc8c
|
data/README.md
CHANGED
@@ -1,3 +1,25 @@
|
|
1
1
|
# Rack::TrafficLogger
|
2
2
|
|
3
|
-
|
3
|
+
This is simple Rack middleware for logging incoming/outgoing HTTP/S traffic.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
```bash
|
8
|
+
gem install rack-traffic-logger
|
9
|
+
```
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
require 'rack/traffic_logger'
|
15
|
+
```
|
16
|
+
|
17
|
+
Then, in your `config.ru` or wherever you set up your middleware stack:
|
18
|
+
|
19
|
+
```ruby
|
20
|
+
use Rack::TrafficLogger, 'path/to/file.log', response_bodies: false, colors: true
|
21
|
+
```
|
22
|
+
|
23
|
+
- If you don't provide a log, everything will go to `STDOUT`.
|
24
|
+
- You can supply either the path to a log file, an open file handle, or an instance of `Logger`.
|
25
|
+
- See [this file](https://github.com/hx/rack-traffic-logger/blob/develop/lib/rack/traffic_logger.rb) for a list of other options that can affect logging style/filtering.
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Rack
|
2
|
+
class TrafficLogger
|
3
|
+
class HeaderHash < Hash
|
4
|
+
|
5
|
+
def initialize(source = nil)
|
6
|
+
source.each { |k, v| self[k] = v } if Hash === source
|
7
|
+
end
|
8
|
+
|
9
|
+
def [](key)
|
10
|
+
super normalize_key key
|
11
|
+
end
|
12
|
+
|
13
|
+
def []=(key, value)
|
14
|
+
super normalize_key(key), value
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def normalize_key(key)
|
20
|
+
key.to_s.split(/[_ -]/).map { |word| word[0].upcase << word[1..-1].downcase }.join('-')
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/rack/traffic_logger.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
1
|
require_relative 'traffic_logger/version'
|
2
2
|
require_relative 'traffic_logger/logger'
|
3
|
+
require_relative 'traffic_logger/header_hash'
|
3
4
|
|
4
5
|
require 'forwardable'
|
5
6
|
require 'rack/nulllogger'
|
@@ -66,11 +67,16 @@ module Rack
|
|
66
67
|
path: env['PATH_INFO'],
|
67
68
|
qs: (q = env['QUERY_STRING']).empty? ? '' : "?#{q}",
|
68
69
|
http: env['HTTP_VERSION'] || 'HTTP/1.1'
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
input
|
70
|
+
if request_headers || request_bodies
|
71
|
+
headers = HeaderHash.new(env_request_headers env)
|
72
|
+
log_headers! headers if request_headers
|
73
|
+
input = env['rack.input']
|
74
|
+
if request_bodies && input
|
75
|
+
log_body! input.read,
|
76
|
+
type: headers['Content-Type'],
|
77
|
+
encoding: headers['Content-Encoding']
|
78
|
+
input.rewind
|
79
|
+
end
|
74
80
|
end
|
75
81
|
end
|
76
82
|
|
@@ -93,11 +99,23 @@ module Rack
|
|
93
99
|
code: code = response[0],
|
94
100
|
status: Rack::Utils::HTTP_STATUS_CODES[code],
|
95
101
|
color: status_color(code)
|
96
|
-
|
97
|
-
|
102
|
+
if response_headers || response_bodies
|
103
|
+
headers = HeaderHash.new(response[1])
|
104
|
+
log_headers! headers if response_headers
|
105
|
+
if response_bodies
|
106
|
+
body = response[2]
|
107
|
+
body = ::File.open(body.path, 'rb') { |f| f.read } if body.respond_to? :path
|
108
|
+
body = body.tap(&:rewind).read if body.respond_to? :read
|
109
|
+
body = body.join if body.respond_to? :join
|
110
|
+
log_body! body,
|
111
|
+
type: headers['Content-Type'],
|
112
|
+
encoding: headers['Content-Encoding']
|
113
|
+
end
|
114
|
+
end
|
98
115
|
end
|
99
116
|
|
100
|
-
def log_body!(body)
|
117
|
+
def log_body!(body, type: nil, encoding: nil)
|
118
|
+
body = "<#BINARY #{body.bytes.length} bytes>" if body =~ /[^[:print:]]/
|
101
119
|
info body
|
102
120
|
end
|
103
121
|
|
@@ -111,16 +129,7 @@ module Rack
|
|
111
129
|
end
|
112
130
|
|
113
131
|
def env_request_headers(env)
|
114
|
-
env.select
|
115
|
-
k =~ /^(CONTENT|HTTP)_(?!VERSION)/
|
116
|
-
end.map do |(k, v)|
|
117
|
-
[
|
118
|
-
k.sub(/^HTTP_/, '').split(/[_ -]/).map do |word|
|
119
|
-
word[0].upcase << word[1..-1].downcase
|
120
|
-
end.join('-'),
|
121
|
-
v
|
122
|
-
]
|
123
|
-
end.to_h
|
132
|
+
env.select { |k, _| k =~ /^(CONTENT|HTTP)_(?!VERSION)/ }.map { |(k, v)| [k.sub(/^HTTP_/, ''), v] }.to_h
|
124
133
|
end
|
125
134
|
|
126
135
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-traffic-logger
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Neil E. Pearson
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rack
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- README.md
|
36
36
|
- lib/rack/traffic_logger.rb
|
37
37
|
- lib/rack/traffic_logger/echo.rb
|
38
|
+
- lib/rack/traffic_logger/header_hash.rb
|
38
39
|
- lib/rack/traffic_logger/logger.rb
|
39
40
|
- lib/rack/traffic_logger/version.rb
|
40
41
|
homepage: https://github.com/hx/rack-traffic-logger
|