rack_silence 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/rack_silence.rb +93 -0
  3. metadata +163 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 64c30a9c95b5d1ff08eb57f4b7181b915a7f6c25
4
+ data.tar.gz: 585719b0de34aab4371503887eb7273a4b695e29
5
+ SHA512:
6
+ metadata.gz: 2e03489bb4f19443fae7a3d7b2a625afbdef866b14c29022592cd88ddd9f7797f7e3d70b16148246fb4caa4edbaaeb6bfcbcdf38120534d62e0da257cb05dec7
7
+ data.tar.gz: b73bac4906311aa12a66bb31dcab4daa14358d9fdc4c4d776eed3bd52c61d3317772cfa219fce5a8c3e09199649a217b0216aa18df61b727a6ea5d83d82ccfbe
@@ -0,0 +1,93 @@
1
+ require 'rack'
2
+
3
+ # Rack middleware that allows silencing select requests
4
+ # by path or header. Token can be set to prevent arbitrary
5
+ # people from silencing logs.
6
+ #
7
+ # Example `config/initializers/rack_silence.rb` for Rails:
8
+ #
9
+ # # inject rack logger in Application class:
10
+ # Rails.application.config
11
+ # .middleware.insert_before(Rails::Rack::Logger, RackSilence::Logger,
12
+ # silenced: [RackSilence.token("decafbad"),
13
+ # '/noisy/action.json',
14
+ # %r{^/uninteresting/[0-9]+}])
15
+ module RackSilence
16
+ # Impose silence on the app's logger
17
+ class Logger
18
+ # @param app Rack app
19
+ # @param logger set logger to control. Make it lazy in a `proc`
20
+ # Defaults to a lazy `Rails.logger`
21
+ # @param header check header `X-SILENCE-LOGGER` before silencing
22
+ # Can be true, false, or `:token`
23
+ # @param silenced array of criterias to silence
24
+ # Can be `String` or `Regexp` to match a path, or `Token` objects
25
+ def initialize(app, opts = {})
26
+ @app = app
27
+ @opts = opts
28
+ @opts[:silenced] ||= []
29
+ @opts[:logger] ||= -> { Rails.logger }
30
+ @opts[:level] ||= ::Logger::ERROR
31
+ end
32
+
33
+ def call(env)
34
+ return @app.call(env) unless silence?(env)
35
+
36
+ silence_new_relic
37
+ silence(logger) { @app.call(env) }
38
+ end
39
+
40
+ protected
41
+
42
+ def silence_new_relic
43
+ NewRelic::Agent.ignore_transaction
44
+ rescue NameError
45
+ nil
46
+ end
47
+
48
+ def silence(logger, temporary_level = @opts[:level])
49
+ previous_level, logger.level = logger.level, temporary_level
50
+ yield
51
+ ensure
52
+ logger.level = previous_level
53
+ end
54
+
55
+ def logger
56
+ @opts[:logger].respond_to?(:call) ? @opts[:logger].call : @opts[:logger]
57
+ end
58
+
59
+ def silence?(env)
60
+ header_silence?(env) || path_silence?(env)
61
+ end
62
+
63
+ def header_silence?(env)
64
+ return unless @opts.key?(:header)
65
+
66
+ if @opts[:header] == :token
67
+ return @opts[:silenced].reduce(false) do |acc, rule|
68
+ acc || rule.is_a?(Token) && rule == env['HTTP_X_SILENCE_LOGGER']
69
+ end
70
+ end
71
+
72
+ env.key?('HTTP_X_SILENCE_LOGGER') if @opts[:header]
73
+ end
74
+
75
+ def path_silence?(env)
76
+ @opts[:silenced].reduce(false) do |acc, rule|
77
+ acc || case rule
78
+ when String then rule == env['PATH_INFO']
79
+ when Regexp then rule.match(env['PATH_INFO'])
80
+ else false
81
+ end
82
+ end
83
+ end
84
+ end
85
+
86
+ # Token value to match against in header
87
+ class Token < String; end
88
+
89
+ # Convenience function to create a token
90
+ def self.token(value)
91
+ Token.new(value)
92
+ end
93
+ end
metadata ADDED
@@ -0,0 +1,163 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_silence
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Loic Nageleisen
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '3.2'
34
+ - - "<"
35
+ - !ruby/object:Gem::Version
36
+ version: '5.0'
37
+ type: :development
38
+ prerelease: false
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: '3.2'
44
+ - - "<"
45
+ - !ruby/object:Gem::Version
46
+ version: '5.0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rake
49
+ requirement: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '10.3'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.3'
61
+ - !ruby/object:Gem::Dependency
62
+ name: rspec
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '3.0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '3.0'
75
+ - !ruby/object:Gem::Dependency
76
+ name: simplecov
77
+ requirement: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ type: :development
83
+ prerelease: false
84
+ version_requirements: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: '0'
89
+ - !ruby/object:Gem::Dependency
90
+ name: rubocop
91
+ requirement: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ type: :development
97
+ prerelease: false
98
+ version_requirements: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '0'
103
+ - !ruby/object:Gem::Dependency
104
+ name: yard
105
+ requirement: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - "~>"
108
+ - !ruby/object:Gem::Version
109
+ version: 0.8.7
110
+ type: :development
111
+ prerelease: false
112
+ version_requirements: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - "~>"
115
+ - !ruby/object:Gem::Version
116
+ version: 0.8.7
117
+ - !ruby/object:Gem::Dependency
118
+ name: pry
119
+ requirement: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ type: :development
125
+ prerelease: false
126
+ version_requirements: !ruby/object:Gem::Requirement
127
+ requirements:
128
+ - - ">="
129
+ - !ruby/object:Gem::Version
130
+ version: '0'
131
+ description: ''
132
+ email: loic.nageleisen@gmail.com
133
+ executables: []
134
+ extensions: []
135
+ extra_rdoc_files: []
136
+ files:
137
+ - lib/rack_silence.rb
138
+ homepage:
139
+ licenses:
140
+ - MIT
141
+ metadata: {}
142
+ post_install_message:
143
+ rdoc_options: []
144
+ require_paths:
145
+ - lib
146
+ required_ruby_version: !ruby/object:Gem::Requirement
147
+ requirements:
148
+ - - ">="
149
+ - !ruby/object:Gem::Version
150
+ version: '0'
151
+ required_rubygems_version: !ruby/object:Gem::Requirement
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ version: '0'
156
+ requirements: []
157
+ rubyforge_project:
158
+ rubygems_version: 2.2.2
159
+ signing_key:
160
+ specification_version: 4
161
+ summary: Silence logs per request from Rack
162
+ test_files: []
163
+ has_rdoc: