rack-traffic-logger 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack/traffic_logger.rb +30 -19
- data/lib/rack/traffic_logger/echo.rb +9 -1
- data/lib/rack/traffic_logger/logger.rb +16 -0
- data/lib/rack/traffic_logger/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2eb0252e73380f08bce1a1fd885b7d1274efb5ea
|
4
|
+
data.tar.gz: 52c68544ff792ba5a11b98d6eef256f5dedf2bf6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3d4540743bb8c876d8dda6196b32d548044bfac62f7736704f2aa7cef441749debaced16d1fb62a6988b89e272bb67e542b8f43755dd35d77a13babd925deb31
|
7
|
+
data.tar.gz: e4486d2c17aff30b74771273df3a5095dce62c1621101dad5549aca5cce7ac8d116a0d47c11395e87c617c874074fe82e73638207a1bd835bcc6986044017d09
|
data/lib/rack/traffic_logger.rb
CHANGED
@@ -51,18 +51,21 @@ module Rack
|
|
51
51
|
@default_options ||= PUBLIC_ATTRIBUTES.map { |k, v| [k, v[:default]] }.to_h
|
52
52
|
end
|
53
53
|
|
54
|
+
def render(template, data)
|
55
|
+
template.gsub(/:(\w+)/) { data[$1.to_sym] }
|
56
|
+
end
|
57
|
+
|
54
58
|
REQUEST_TEMPLATES = {
|
55
|
-
true => "
|
56
|
-
false =>
|
59
|
+
true => "\e[35m:verb \e[36m:path:qs\e[0m :http",
|
60
|
+
false => ':verb :path:qs :http'
|
57
61
|
}
|
58
62
|
|
59
63
|
def log_request!(env)
|
60
|
-
debug REQUEST_TEMPLATES[colors]
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
]
|
64
|
+
debug render REQUEST_TEMPLATES[colors],
|
65
|
+
verb: env['REQUEST_METHOD'],
|
66
|
+
path: env['PATH_INFO'],
|
67
|
+
qs: (q = env['QUERY_STRING']).empty? ? '' : "?#{q}",
|
68
|
+
http: env['HTTP_VERSION'] || 'HTTP/1.1'
|
66
69
|
log_headers! env_request_headers(env) if request_headers
|
67
70
|
input = env['rack.input']
|
68
71
|
if request_bodies && input
|
@@ -72,16 +75,24 @@ module Rack
|
|
72
75
|
end
|
73
76
|
|
74
77
|
RESPONSE_TEMPLATES = {
|
75
|
-
true => "
|
76
|
-
false =>
|
78
|
+
true => ":http \e[:color:code \e[36m:status\e[0m",
|
79
|
+
false => ':http :code :status'
|
77
80
|
}
|
78
81
|
|
82
|
+
def status_color(status)
|
83
|
+
case (status / 100).to_i
|
84
|
+
when 2 then '32m'
|
85
|
+
when 4, 5 then '31m'
|
86
|
+
else '33m'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
79
90
|
def log_response!(env, response)
|
80
|
-
debug RESPONSE_TEMPLATES[colors]
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
91
|
+
debug render RESPONSE_TEMPLATES[colors],
|
92
|
+
http: env['HTTP_VERSION'] || 'HTTP/1.1',
|
93
|
+
code: code = response[0],
|
94
|
+
status: Rack::Utils::HTTP_STATUS_CODES[code],
|
95
|
+
color: status_color(code)
|
85
96
|
log_headers! response[1] if response_headers
|
86
97
|
log_body! response[2].join if response_bodies
|
87
98
|
end
|
@@ -91,17 +102,17 @@ module Rack
|
|
91
102
|
end
|
92
103
|
|
93
104
|
HEADER_TEMPLATES = {
|
94
|
-
true => "
|
95
|
-
false => "
|
105
|
+
true => "\e[4m:key\e[0m: :val\n",
|
106
|
+
false => ":key: :val\n"
|
96
107
|
}
|
97
108
|
|
98
109
|
def log_headers!(headers)
|
99
|
-
info headers.map { |k, v| HEADER_TEMPLATES[colors]
|
110
|
+
info headers.map { |k, v| render HEADER_TEMPLATES[colors], key: k, val: v }.join
|
100
111
|
end
|
101
112
|
|
102
113
|
def env_request_headers(env)
|
103
114
|
env.select do |k, _|
|
104
|
-
k =~ /^(CONTENT|HTTP)_/
|
115
|
+
k =~ /^(CONTENT|HTTP)_(?!VERSION)/
|
105
116
|
end.map do |(k, v)|
|
106
117
|
[
|
107
118
|
k.sub(/^HTTP_/, '').split(/[_ -]/).map do |word|
|
@@ -5,7 +5,15 @@ module Rack
|
|
5
5
|
class TrafficLogger
|
6
6
|
class Echo
|
7
7
|
def call(env)
|
8
|
-
|
8
|
+
begin
|
9
|
+
JSON.parse env['rack.input'].tap(&:rewind).read
|
10
|
+
rescue JSON::ParserError => error
|
11
|
+
[
|
12
|
+
500,
|
13
|
+
{'Content-Type' => 'text/plain;charset=UTF-8'},
|
14
|
+
[error.message]
|
15
|
+
]
|
16
|
+
end
|
9
17
|
end
|
10
18
|
end
|
11
19
|
end
|
@@ -4,6 +4,22 @@ module Rack
|
|
4
4
|
class TrafficLogger
|
5
5
|
class Logger < ::Logger
|
6
6
|
|
7
|
+
def initialize(*args)
|
8
|
+
super *args
|
9
|
+
@default_formatter = Formatter.new
|
10
|
+
end
|
11
|
+
|
12
|
+
class Formatter
|
13
|
+
|
14
|
+
def call(severity, time, progname, msg)
|
15
|
+
if severity == 'INFO'
|
16
|
+
"#{msg}\n"
|
17
|
+
else
|
18
|
+
"@ #{time.strftime '%a %d %b \'%y %T'}.#{'%d' % (time.usec / 1e4)}\n#{msg}\n"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
7
23
|
end
|
8
24
|
end
|
9
25
|
end
|