logger_ware 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ bin
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color --format nested
data/Changelog ADDED
@@ -0,0 +1,3 @@
1
+ # 0.0.1
2
+
3
+ initial version
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in logger_ware.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,10 @@
1
+ guard 'bundler' do
2
+ watch 'Gemfile'
3
+ watch 'logger_ware.gemspec'
4
+ end
5
+
6
+ guard :rspec, cli: '--color --format nested' do
7
+ watch(%r{^spec/.+_spec\.rb$})
8
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
9
+ watch('spec/spec_helper.rb') { "spec" }
10
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Vitaly Kushner
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # LoggerWare
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'logger_ware'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install logger_ware
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,40 @@
1
+ require 'logger_ware/filter'
2
+
3
+ module LoggerWare
4
+ module Collector
5
+ include Filter
6
+
7
+ COLLECT_PARAMS = {
8
+ request_path: 'REQUEST_PATH',
9
+ method: 'REQUEST_METHOD',
10
+ request_uri: 'REQUEST_URI',
11
+ user_agent: 'HTTP_USER_AGENT',
12
+ http_accept: 'HTTP_ACCEPT',
13
+ rack_session: "rack.session",
14
+ request_ip: 'REMOTE_ADDR',
15
+ request_ip_fw: 'HTTP_X_FORWARDED_FOR',
16
+ referer: 'HTTP_REFERER',
17
+ }
18
+ FILTER_PARAMS = {
19
+ request_parameters: 'action_controller.request.request_parameters',
20
+ path_parameters: 'action_controller.request.path_parameters',
21
+ query_parameters: 'action_controller.request.query_parameters',
22
+ }
23
+
24
+ PARAM_FILTERS = [/password/]
25
+
26
+ def _collect env, keys, filters = nil, replace_with = nil
27
+ keys.each_with_object({}) do |(k,v), res|
28
+ next unless env.has_key?(v)
29
+ v = env[v]
30
+ v = filter(v, filters, replace_with) if filters
31
+ res[k] = v
32
+ end
33
+ end
34
+
35
+ def collect env, collect_params = COLLECT_PARAMS, filter_params = FILTER_PARAMS, filters = PARAM_FILTERS, replace_with = Filter::REPLACE_WITH
36
+ _collect(env, collect_params).merge(_collect(env, filter_params, filters, replace_with))
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,25 @@
1
+ module LoggerWare
2
+ module Filter
3
+ REPLACE_WITH = '[FILTERED]'
4
+
5
+ def match?(param, f)
6
+ if f.is_a?(Regexp)
7
+ !!(param.to_s =~ f)
8
+ else
9
+ param.to_s == f.to_s
10
+ end
11
+ end
12
+
13
+ def filter?(param, filters)
14
+ filters.each { |f| return true if match?(param, f) }
15
+ false
16
+ end
17
+
18
+ def filter(params, filters, replace_with = REPLACE_WITH)
19
+ params.each_with_object({}) do |(k,v), res|
20
+ v = filter(v, filters, replace_with) if v.is_a?(Hash)
21
+ res[k] = filter?(k, filters) ? replace_with : v
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,49 @@
1
+ require 'rack'
2
+ require 'logger_ware/collector'
3
+
4
+ module LoggerWare
5
+ class Rack
6
+ include Collector
7
+
8
+ @collect_params = COLLECT_PARAMS
9
+ @filter_params = FILTER_PARAMS
10
+ @replace_with = REPLACE_WITH
11
+ @filters = PARAM_FILTERS
12
+ @handler = ->(_) {raise 'missing handler, please assign'}
13
+ @error_handler = ->(_) {raise 'missing error_handler, please assign'}
14
+
15
+ class << self
16
+ attr_accessor :collect_params, :filter_params, :replace_with, :filters, :handler, :error_handler
17
+ end
18
+
19
+ [:collect_params, :filter_params, :replace_with, :filters, :handler, :error_handler].each do |m|
20
+ define_method(m) { |*args| self.class.send(m, *args) }
21
+ end
22
+
23
+ def initialize(app)
24
+ @app = app
25
+ end
26
+
27
+ def call(env)
28
+ started_at = Time.now.utc
29
+
30
+ status, headers, response = @app.call(env)
31
+
32
+ handler[{status: status, headers: headers, response: response, started_at: started_at, duration: duration(started_at), data: data(env)}]
33
+ [status, headers, response]
34
+
35
+ rescue => exception
36
+
37
+ error_handler[{exception: exception, started_at: started_at, duration: duration(started_at), data: data(env)}]
38
+ raise exception
39
+ end
40
+
41
+ def data(env)
42
+ collect(env, collect_params, filter_params, filters, replace_with)
43
+ end
44
+
45
+ def duration(started_at)
46
+ Time.now.utc - started_at
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module LoggerWare
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "logger_ware/version"
2
+ require 'logger_ware/rack'
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'logger_ware/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "logger_ware"
8
+ spec.version = LoggerWare::VERSION
9
+ spec.authors = ["Vitaly Kushner"]
10
+ spec.email = ["vitaly@astrails.com"]
11
+ spec.summary = "middleware to log rails requests"
12
+ spec.description = %q{Rails middleware to log requests. Support for parameters and environment filtering. Storage agnostic.}
13
+ spec.homepage = "http://github.com/astrails/logger_ware"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'rack'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency 'debugger'
26
+ spec.add_development_dependency "rspec"
27
+ spec.add_development_dependency "guard-rspec"
28
+ spec.add_development_dependency "guard-bundler"
29
+ spec.add_development_dependency 'terminal-notifier-guard'
30
+ spec.add_development_dependency 'rb-fchange'
31
+ spec.add_development_dependency 'rb-fsevent'
32
+ spec.add_development_dependency 'rb-inotify'
33
+ end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe LoggerWare::Collector do
4
+ include LoggerWare::Collector
5
+
6
+ E = {
7
+ 'A' => 'a',
8
+ 'B' => {'C' => 'c', 'D' => 'd'},
9
+ 'F' => false,
10
+ 'N' => nil,
11
+ }
12
+
13
+ describe :collect do
14
+ it 'should return empty hash when no params match' do
15
+ collect(E).should == {}
16
+ end
17
+
18
+ it 'should collect params' do
19
+ collect(E, {a: 'A'}).should == {a: 'a'}
20
+ end
21
+
22
+ it 'should collect false and nil' do
23
+ collect(E, {f: 'F', n: 'N'}).should == {f: false, n: nil}
24
+ end
25
+
26
+ it 'should filter' do
27
+ collect(E, {a: 'A'}, {b: 'B'}, [/D/], 'XXX').should == {
28
+ a: 'a',
29
+ b: {'C' => 'c', 'D' => 'XXX'}
30
+ }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,83 @@
1
+ require 'spec_helper'
2
+
3
+ describe LoggerWare::Filter do
4
+ include LoggerWare::Filter
5
+
6
+ describe :match? do
7
+ it 'should NOT match nil filter' do
8
+ match?('foo', nil).should == false
9
+ end
10
+
11
+ describe 'string' do
12
+ it 'should match string filter' do
13
+ match?('foo', 'foo').should == true
14
+ end
15
+
16
+ it 'should match symbol filter' do
17
+ match?('foo', :foo).should == true
18
+ end
19
+
20
+ it 'should match regexp filter' do
21
+ match?('foo', /o/).should == true
22
+ end
23
+ end
24
+
25
+ describe :symbol do
26
+ it 'should match string filter' do
27
+ match?(:foo, 'foo').should == true
28
+ end
29
+
30
+ it 'should match symbol filter' do
31
+ match?(:foo, :foo).should == true
32
+ end
33
+
34
+ it 'should match regexp filter' do
35
+ match?(:foo, /o/).should == true
36
+ end
37
+ end
38
+ end
39
+
40
+ describe :filter? do
41
+ it 'should return arg when filters are empty' do
42
+ filter?('foo', []).should == false
43
+ end
44
+
45
+ it 'should filter out param by string' do
46
+ filter?('foo', ['foo']).should == true
47
+ end
48
+
49
+ it 'should filter out symbol param by string' do
50
+ filter?(:foo, ['foo']).should == true
51
+ end
52
+
53
+ it 'should filter out param by symbol' do
54
+ filter?('foo', [:foo]).should == true
55
+ end
56
+
57
+ it 'should filter out param by regexp' do
58
+ filter?('foo', [/fo/]).should == true
59
+ end
60
+
61
+ it 'should filter out symbol param by regexp' do
62
+ filter?(:foo, [/fo/]).should == true
63
+ end
64
+ end
65
+
66
+ describe :filter do
67
+ it 'should return params on empty filters' do
68
+ filter({foo: 123}, []).should == {foo: 123}
69
+ end
70
+
71
+ it 'should filter 1st level keys' do
72
+ filter({foo: 123, bar: 456}, [:foo]).should == {foo: '[FILTERED]', bar: 456}
73
+ end
74
+
75
+ it 'should filter 2nd level keys' do
76
+ filter({foo: 123, bar: 456, baz: {ooo: 1}}, [/o/]).should == {foo: '[FILTERED]', bar: 456, baz: {ooo: '[FILTERED]'}}
77
+ end
78
+
79
+ it 'should use supplied dummy' do
80
+ filter({foo: 123}, [:foo], 'X').should == {foo: 'X'}
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe LoggerWare::Rack do
4
+ class App
5
+ def call(env)
6
+ [200, {a: 'b'}, 'ok']
7
+ end
8
+ end
9
+
10
+ class ErrApp
11
+ def call(env)
12
+ raise 'foo'
13
+ end
14
+ end
15
+
16
+ it 'should allow setting filters' do
17
+ LoggerWare::Rack.filters.should == [/password/]
18
+ LoggerWare::Rack.filters << 'qwe'
19
+ LoggerWare::Rack.filters.should == [/password/, 'qwe']
20
+ end
21
+
22
+ it 'should call handler' do
23
+ opts = nil
24
+ LoggerWare::Rack.handler = ->(args) {opts = args}
25
+ LoggerWare::Rack.new(App.new).call({}).should == [200, {a: 'b'}, 'ok']
26
+ opts[:status].should == 200
27
+ opts[:response].should == 'ok'
28
+ opts.keys.should == [:status, :headers, :response, :started_at, :duration, :data]
29
+ end
30
+
31
+ it 'should call error handler' do
32
+ opts = nil
33
+ LoggerWare::Rack.handler = ->(args) {opts = args}
34
+ err = nil
35
+ LoggerWare::Rack.error_handler = ->(args) {err = args}
36
+ -> {
37
+ LoggerWare::Rack.new(ErrApp.new).call({})
38
+ }.should raise_error('foo')
39
+ err[:exception].to_s.should == 'foo'
40
+ end
41
+ end
@@ -0,0 +1,7 @@
1
+ require 'debugger'
2
+ require 'logger_ware'
3
+
4
+ Dir["spec/support/**/*.rb"].each { |f| require File.expand_path(f) }
5
+
6
+ RSpec.configure do |config|
7
+ end
metadata ADDED
@@ -0,0 +1,251 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logger_ware
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - Vitaly Kushner
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-09-10 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ type: :runtime
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ none: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ! '>='
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ none: false
29
+ prerelease: false
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ type: :development
33
+ requirement: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.3'
38
+ none: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ version: '1.3'
44
+ none: false
45
+ prerelease: false
46
+ - !ruby/object:Gem::Dependency
47
+ name: rake
48
+ type: :development
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ none: false
55
+ version_requirements: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ! '>='
58
+ - !ruby/object:Gem::Version
59
+ version: '0'
60
+ none: false
61
+ prerelease: false
62
+ - !ruby/object:Gem::Dependency
63
+ name: debugger
64
+ type: :development
65
+ requirement: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ none: false
71
+ version_requirements: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ! '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ none: false
77
+ prerelease: false
78
+ - !ruby/object:Gem::Dependency
79
+ name: rspec
80
+ type: :development
81
+ requirement: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ none: false
87
+ version_requirements: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ none: false
93
+ prerelease: false
94
+ - !ruby/object:Gem::Dependency
95
+ name: guard-rspec
96
+ type: :development
97
+ requirement: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ none: false
103
+ version_requirements: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ! '>='
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ none: false
109
+ prerelease: false
110
+ - !ruby/object:Gem::Dependency
111
+ name: guard-bundler
112
+ type: :development
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ! '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ none: false
119
+ version_requirements: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ! '>='
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ none: false
125
+ prerelease: false
126
+ - !ruby/object:Gem::Dependency
127
+ name: terminal-notifier-guard
128
+ type: :development
129
+ requirement: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ! '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ none: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ none: false
141
+ prerelease: false
142
+ - !ruby/object:Gem::Dependency
143
+ name: rb-fchange
144
+ type: :development
145
+ requirement: !ruby/object:Gem::Requirement
146
+ requirements:
147
+ - - ! '>='
148
+ - !ruby/object:Gem::Version
149
+ version: '0'
150
+ none: false
151
+ version_requirements: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ! '>='
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ none: false
157
+ prerelease: false
158
+ - !ruby/object:Gem::Dependency
159
+ name: rb-fsevent
160
+ type: :development
161
+ requirement: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ! '>='
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ none: false
167
+ version_requirements: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ! '>='
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ none: false
173
+ prerelease: false
174
+ - !ruby/object:Gem::Dependency
175
+ name: rb-inotify
176
+ type: :development
177
+ requirement: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ! '>='
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ none: false
183
+ version_requirements: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - ! '>='
186
+ - !ruby/object:Gem::Version
187
+ version: '0'
188
+ none: false
189
+ prerelease: false
190
+ description: Rails middleware to log requests. Support for parameters and environment
191
+ filtering. Storage agnostic.
192
+ email:
193
+ - vitaly@astrails.com
194
+ executables: []
195
+ extensions: []
196
+ extra_rdoc_files: []
197
+ files:
198
+ - .gitignore
199
+ - .rspec
200
+ - Changelog
201
+ - Gemfile
202
+ - Guardfile
203
+ - LICENSE.txt
204
+ - README.md
205
+ - Rakefile
206
+ - lib/logger_ware.rb
207
+ - lib/logger_ware/collector.rb
208
+ - lib/logger_ware/filter.rb
209
+ - lib/logger_ware/rack.rb
210
+ - lib/logger_ware/version.rb
211
+ - logger_ware.gemspec
212
+ - spec/lib/logger_ware/collector_spec.rb
213
+ - spec/lib/logger_ware/filter_spec.rb
214
+ - spec/lib/logger_ware/rack_spec.rb
215
+ - spec/spec_helper.rb
216
+ homepage: http://github.com/astrails/logger_ware
217
+ licenses:
218
+ - MIT
219
+ post_install_message:
220
+ rdoc_options: []
221
+ require_paths:
222
+ - lib
223
+ required_ruby_version: !ruby/object:Gem::Requirement
224
+ requirements:
225
+ - - ! '>='
226
+ - !ruby/object:Gem::Version
227
+ segments:
228
+ - 0
229
+ hash: 2159498929020429373
230
+ version: '0'
231
+ none: false
232
+ required_rubygems_version: !ruby/object:Gem::Requirement
233
+ requirements:
234
+ - - ! '>='
235
+ - !ruby/object:Gem::Version
236
+ segments:
237
+ - 0
238
+ hash: 2159498929020429373
239
+ version: '0'
240
+ none: false
241
+ requirements: []
242
+ rubyforge_project:
243
+ rubygems_version: 1.8.25
244
+ signing_key:
245
+ specification_version: 3
246
+ summary: middleware to log rails requests
247
+ test_files:
248
+ - spec/lib/logger_ware/collector_spec.rb
249
+ - spec/lib/logger_ware/filter_spec.rb
250
+ - spec/lib/logger_ware/rack_spec.rb
251
+ - spec/spec_helper.rb