rack-logjam 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/rack/logjam/middleware.rb +115 -104
- data/lib/rack/logjam/version.rb +1 -1
- data/lib/rack/logjam.rb +6 -3
- data/lib/rack-logjam.rb +1 -0
- data/rack-logjam.gemspec +4 -0
- metadata +44 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 73b701b47ef0a2b508486982ca4f8feb85c52c98
|
4
|
+
data.tar.gz: 4484c3413f56d8e829da75ac23c9018c26d45d08
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b571b14bd4f51e7efb8d8ea1e103c05b2b42c316a3f0af7a04f19c9df8bb1e996a998b1ab487f7524cca60693de8e446db3df170d4706320aac1f581c7aefbd9
|
7
|
+
data.tar.gz: f73607ebeaf91bbf78634416e56d95f9bc3eab977a1f3c5adbf920d563ed83a564c3747936ad99f5fd7e935a4320d33d0a5fc12cc3aef223f50a406a16698a3d
|
@@ -1,137 +1,148 @@
|
|
1
|
-
|
1
|
+
require 'action_dispatch/http/mime_type'
|
2
|
+
require 'jsonpath'
|
2
3
|
|
3
|
-
|
4
|
-
@app = app
|
5
|
-
end
|
4
|
+
module Rack
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
6
|
+
module Logjam
|
7
|
+
|
8
|
+
class Middleware
|
9
|
+
|
10
|
+
def initialize( app )
|
11
|
+
@app = app
|
12
|
+
end
|
13
|
+
|
14
|
+
def call( env )
|
15
|
+
before env
|
16
|
+
app.call( env ).tap do |rack_response|
|
17
|
+
after env, *rack_response
|
18
|
+
end
|
19
|
+
end
|
13
20
|
|
14
|
-
protected
|
21
|
+
protected
|
15
22
|
|
16
|
-
|
23
|
+
attr_reader :app
|
17
24
|
|
18
|
-
|
19
|
-
|
25
|
+
def before( env )
|
26
|
+
return unless api_request?( env )
|
20
27
|
|
21
|
-
|
28
|
+
Rails.logger.info <<-end_info
|
22
29
|
[#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Request Env ---' }}
|
23
30
|
#{ANSI.magenta { JSON.pretty_generate( request_log_data( env )) }}
|
24
31
|
[#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Request Body ---' }}
|
25
32
|
#{ANSI.cyan { formatted_request_body( env ) }}
|
26
|
-
|
27
|
-
|
33
|
+
end_info
|
34
|
+
end
|
28
35
|
|
29
|
-
|
30
|
-
|
36
|
+
def after( env, status, headers, response )
|
37
|
+
return unless api_request?( env )
|
31
38
|
|
32
|
-
|
39
|
+
Rails.logger.info <<-end_info
|
33
40
|
[#{ANSI.green { 'api' }}] #{ANSI.cyan { '--- Response ---' }}
|
34
41
|
Status: #{status}
|
35
42
|
Headers: #{headers.inspect}
|
36
43
|
Body:
|
37
44
|
#{ANSI.cyan { format_body( response.body, accept( env ), env ) }}
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
def request_log_data( env )
|
42
|
-
request_data = {
|
43
|
-
auth_token: env['HTTP_X_NCITE_AUTH_TOKEN'],
|
44
|
-
content_type: content_type( env ),
|
45
|
-
content_length: env['CONTENT_LENGTH'],
|
46
|
-
accept: accept( env ),
|
47
|
-
accept_version: env['HTTP_ACCEPT_VERSION'],
|
48
|
-
method: env['REQUEST_METHOD'],
|
49
|
-
path: path_info( env ),
|
50
|
-
query: query( env )
|
51
|
-
}
|
52
|
-
#request_data[:user_id] = current_user.id if current_user
|
53
|
-
request_data
|
54
|
-
end
|
45
|
+
end_info
|
46
|
+
end
|
55
47
|
|
56
|
-
|
57
|
-
|
58
|
-
|
48
|
+
def request_log_data( env )
|
49
|
+
request_data = {
|
50
|
+
auth_token: env['HTTP_X_NCITE_AUTH_TOKEN'],
|
51
|
+
content_type: content_type( env ),
|
52
|
+
content_length: env['CONTENT_LENGTH'],
|
53
|
+
accept: accept( env ),
|
54
|
+
accept_version: env['HTTP_ACCEPT_VERSION'],
|
55
|
+
method: env['REQUEST_METHOD'],
|
56
|
+
path: path_info( env ),
|
57
|
+
query: query( env )
|
58
|
+
}
|
59
|
+
#request_data[:user_id] = current_user.id if current_user
|
60
|
+
request_data
|
61
|
+
end
|
59
62
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
+
def api_request?( env )
|
64
|
+
path_info( env ) =~ /^\/api\//
|
65
|
+
end
|
63
66
|
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
+
def content_type( env )
|
68
|
+
env['CONTENT_TYPE']
|
69
|
+
end
|
67
70
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
+
def accept( env )
|
72
|
+
env['HTTP_ACCEPT']
|
73
|
+
end
|
71
74
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
+
def path_info( env )
|
76
|
+
env['PATH_INFO']
|
77
|
+
end
|
75
78
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
+
def query( env )
|
80
|
+
URI.unescape( env['QUERY_STRING'] )
|
81
|
+
end
|
79
82
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
if format == Mime::JSON.to_s
|
84
|
-
hash = truncate_json_attributes( body )
|
85
|
-
return JSON.pretty_generate( hash )
|
86
|
-
elsif format == Mime::XML.to_s
|
87
|
-
return Nokogiri.XML( body ) do |config|
|
88
|
-
config.default_xml.noblanks
|
89
|
-
end.to_xml( indent: 2 )
|
90
|
-
elsif format == Mime::URL_ENCODED_FORM.to_s
|
91
|
-
return URI.unescape( body )
|
92
|
-
elsif format == Mime::OCTET_STREAM.to_s
|
93
|
-
return "no body b/c content type is #{Mime::OCTET_STREAM}"
|
94
|
-
end
|
83
|
+
def formatted_request_body( env )
|
84
|
+
format_body( rack_input_content( env ), content_type( env ), env )
|
85
|
+
end
|
95
86
|
|
96
|
-
|
97
|
-
|
87
|
+
def format_body( body, format, env )
|
88
|
+
return body if body.strip.nil? || body.strip.empty?
|
89
|
+
|
90
|
+
if format == ::Mime::JSON.to_s
|
91
|
+
hash = truncate_json_attributes( body )
|
92
|
+
return ::JSON.pretty_generate( hash )
|
93
|
+
elsif format == ::Mime::XML.to_s
|
94
|
+
return ::Nokogiri.XML( body ) do |config|
|
95
|
+
config.default_xml.noblanks
|
96
|
+
end.to_xml( indent: 2 )
|
97
|
+
elsif format == ::Mime::URL_ENCODED_FORM.to_s
|
98
|
+
return URI.unescape( body )
|
99
|
+
elsif format == ::Mime::OCTET_STREAM.to_s
|
100
|
+
return "no body b/c content type is #{::Mime::OCTET_STREAM}"
|
101
|
+
end
|
102
|
+
|
103
|
+
body
|
104
|
+
end
|
98
105
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
106
|
+
def rack_input_content( env )
|
107
|
+
( rack_input = env['rack.input'] ).read.tap do |content|
|
108
|
+
rack_input.rewind
|
109
|
+
end
|
110
|
+
end
|
104
111
|
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
112
|
+
# Can currently use the following xpath expressions that will translate to json_path:
|
113
|
+
# /some/path -> $.some.path (search from root)
|
114
|
+
# some/path -> some.path (search for any sub-path that matches this, not bound to root)
|
115
|
+
# //path -> $..path (recursive search)
|
116
|
+
#
|
117
|
+
def truncated_attributes_as_xpath
|
118
|
+
%w(
|
119
|
+
//image_as_base64
|
120
|
+
//fingerprint_image_as_base64
|
121
|
+
)
|
122
|
+
end
|
116
123
|
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
+
def truncate_json_attributes( json )
|
125
|
+
json_path = ::JsonPath.for( json )
|
126
|
+
truncated_attributes_as_json_paths.each do |j_path|
|
127
|
+
json_path.gsub!( j_path ) { |val| (val.nil? || val.empty?) ? val : "#{val[0..25]} ... [TRUNCATED] ..." }
|
128
|
+
end
|
129
|
+
json_path.to_hash
|
130
|
+
end
|
124
131
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
132
|
+
def truncated_attributes_as_json_paths
|
133
|
+
truncated_attributes_as_xpath.map do |xpath|
|
134
|
+
if xpath.start_with?( '//' )
|
135
|
+
xpath.gsub( '//', '$..' )
|
136
|
+
else
|
137
|
+
parts = xpath.split( '/' )
|
138
|
+
parts[0] = '$' if parts[0] == ''
|
139
|
+
parts.join( '.' )
|
140
|
+
end
|
141
|
+
end
|
133
142
|
end
|
143
|
+
|
134
144
|
end
|
145
|
+
|
135
146
|
end
|
136
147
|
|
137
|
-
end
|
148
|
+
end
|
data/lib/rack/logjam/version.rb
CHANGED
data/lib/rack/logjam.rb
CHANGED
data/lib/rack-logjam.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'rack/logjam'
|
data/rack-logjam.gemspec
CHANGED
@@ -20,6 +20,10 @@ Gem::Specification.new do |spec|
|
|
20
20
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
21
21
|
spec.require_paths = ['lib']
|
22
22
|
|
23
|
+
spec.add_dependency 'actionpack', '~> 3.2'
|
24
|
+
spec.add_dependency 'jsonpath', '~> 0.5'
|
25
|
+
spec.add_dependency 'nokogiri', '~> 1.6'
|
26
|
+
|
23
27
|
spec.add_development_dependency 'bundler', '~> 1.5'
|
24
28
|
spec.add_development_dependency 'rake'
|
25
29
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rack-logjam
|
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
|
- Jason Harrelson
|
@@ -11,6 +11,48 @@ bindir: bin
|
|
11
11
|
cert_chain: []
|
12
12
|
date: 2014-02-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionpack
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - "~>"
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '3.2'
|
21
|
+
type: :runtime
|
22
|
+
prerelease: false
|
23
|
+
version_requirements: !ruby/object:Gem::Requirement
|
24
|
+
requirements:
|
25
|
+
- - "~>"
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
version: '3.2'
|
28
|
+
- !ruby/object:Gem::Dependency
|
29
|
+
name: jsonpath
|
30
|
+
requirement: !ruby/object:Gem::Requirement
|
31
|
+
requirements:
|
32
|
+
- - "~>"
|
33
|
+
- !ruby/object:Gem::Version
|
34
|
+
version: '0.5'
|
35
|
+
type: :runtime
|
36
|
+
prerelease: false
|
37
|
+
version_requirements: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - "~>"
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0.5'
|
42
|
+
- !ruby/object:Gem::Dependency
|
43
|
+
name: nokogiri
|
44
|
+
requirement: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - "~>"
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '1.6'
|
49
|
+
type: :runtime
|
50
|
+
prerelease: false
|
51
|
+
version_requirements: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - "~>"
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '1.6'
|
14
56
|
- !ruby/object:Gem::Dependency
|
15
57
|
name: bundler
|
16
58
|
requirement: !ruby/object:Gem::Requirement
|
@@ -51,6 +93,7 @@ files:
|
|
51
93
|
- License.markdown
|
52
94
|
- README.markdown
|
53
95
|
- Rakefile
|
96
|
+
- lib/rack-logjam.rb
|
54
97
|
- lib/rack/logjam.rb
|
55
98
|
- lib/rack/logjam/middleware.rb
|
56
99
|
- lib/rack/logjam/version.rb
|