silencer 1.0.0.rc3 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 6c3957c09e55e6fcaa4028e2cfadbe5f356c9948
4
- data.tar.gz: 42c65e23eb1d156e0566b3a19251fd88f4cec662
2
+ SHA256:
3
+ metadata.gz: 63363db7eb9e93e891e6910a11e53919acfaab80e63a280544e9b7c1b3eed686
4
+ data.tar.gz: 347c68c4cf21b860ab12497a684b5b037f980ef1649ae9bdc03fda713870703e
5
5
  SHA512:
6
- metadata.gz: 007f356080f9a3a0fa4698a968fd21434db70dfc809c4643ba64e78bf43eb45cec69684579efb15ed6da875690683d8069b0a66799e397ea4c9d513317d0b6c9
7
- data.tar.gz: 496d3ee52a8d11aa9d2ce9d3eff1805716327b474025f23302aa3a92f9f692c59b5dae76e967c120b06e4c5154496e61c2bd903d2f2984e18ce7829aea4d8e2d
6
+ metadata.gz: b631a27a0bb92c343ff55f5976416b44d23cbe9f520d412890c98ba1c7d2489a283404d8b0d70a2ac2ee0278d5578f52affcc37d7b891809e8907aaac96a85de
7
+ data.tar.gz: 4435eb5f9254a7fd2266947dcef3b4776b41e8e1579edab6adb2a550d6bd2c6f5b5903e596dfa741a9bbdda21b76c68dbe30ec40bf8e13c7bae557b8ab90d731
data/README.md CHANGED
@@ -1,64 +1,104 @@
1
- # Silencer [![Build Status](https://secure.travis-ci.org/spagalloco/silencer.png?branch=master)][travis] [![Dependency Status](https://gemnasium.com/spagalloco/silencer.png?travis)][gemnasium]
1
+ # Silencer
2
2
 
3
- [travis]: http://travis-ci.org/spagalloco/silencer
4
- [gemnasium]: https://gemnasium.com/spagalloco/silencer
3
+ [![Gem Version](http://img.shields.io/gem/v/silencer.svg)][gem]
4
+ ![Tests](https://github.com/stve/silencer/actions/workflows/ci.yml/badge.svg)
5
+
6
+ [gem]: https://rubygems.org/gems/silencer
5
7
 
6
8
  Silencer is a simple rack-middleware for Rails that can selectively disable logging on per-action basis. It's based on a [blog post](http://dennisreimann.de/blog/silencing-the-rails-log-on-a-per-action-basis/) by Dennis Reimann.
7
9
 
8
- __Note__: Silencer is not thread safe.
10
+ __Note__: Silencer is only threadsafe in Rails version 4.2.6 and later.
9
11
 
10
12
  ## Installation
11
13
 
12
14
  Just add silencer to your Gemfile:
13
15
 
14
- gem 'silencer'
16
+ ```ruby
17
+ gem 'silencer', require: false
18
+ ```
15
19
 
16
- ## Usage
20
+ ### Upgrading to version 2.0
17
21
 
18
- In your production environment (presumably):
22
+ __Note:__ 1.x versions of silencer detected the presence of Rails when it was required. This made it easy to use silencer for most applications, but made assumptions on usage.
19
23
 
24
+ In version 2.0, you'll need to require the correct logger within your application. See the Usage documentation for more details.
20
25
 
21
- require 'silencer/logger'
26
+ ## Usage
22
27
 
23
- config.middleware.swap Rails::Rack::Logger, Silencer::Logger, :silence => ["/noisy/action.json"]
28
+ ### Rails
24
29
 
25
- Or if you'd prefer, you can pass it regular expressions:
30
+ Create an initializer (like `config/initializers/silencer.rb`) with the contents:
26
31
 
32
+ ```ruby
33
+ require 'silencer/rails/logger'
27
34
 
28
- config.middleware.swap Rails::Rack::Logger, Silencer::Logger, :silence => [%r{^/assets/}]
35
+ Rails.application.configure do
36
+ config.middleware.swap(
37
+ Rails::Rack::Logger,
38
+ Silencer::Logger,
39
+ config.log_tags,
40
+ silence: ["/noisy/action.json"]
41
+ )
42
+ end
43
+ ```
29
44
 
30
- Or you can silence specific request methods only:
45
+ ### Rack
31
46
 
47
+ ```ruby
48
+ require 'silencer/rack/logger'
32
49
 
33
- config.middleware.swap Rails::Rack::Logger, Silencer::Logger, :get => [%r{^/assets/}], :post => [%r{^/some_path}]
50
+ use Silencer::Logger, silence: ["/noisy/action.json"]
51
+ ```
34
52
 
35
- Silencer's logger will serve as a drop-in replacement for Rails' default logger. It will not suppress any logging by default, simply pass it an array of urls via the options hash. You can also send it a 'X-SILENCE-LOGGER' header (with any value) with your request and that will also produce the same behavior.
53
+ ## Configuration
36
54
 
37
- ### All options
55
+ Or if you'd prefer, you can pass it regular expressions:
38
56
 
39
- Silencer supports the following configuration options.
57
+ ```ruby
58
+ config.middleware.swap(
59
+ Rails::Rack::Logger,
60
+ Silencer::Logger,
61
+ config.log_tags,
62
+ silence: [%r{^/assets/}]
63
+ )
64
+ ```
65
+
66
+ Or you can silence specific request methods only:
40
67
 
41
- :silence - Silences matching requests regardless of request method
42
- :get - Silences matching GET requests
43
- :head - Silences matching HEAD requests
44
- :post - Silences matching POST requests
45
- :put - Silences matching PUT requests
46
- :delete - Silences matching DELETE requests
47
- :patch - Silences matching PATCH requests
48
- :trace - Silences matching TRACE requests
49
- :connect - Silences matching CONNECT requests
50
- :options - Silences matching OPTIONS requests
68
+ ```ruby
69
+ config.middleware.swap(
70
+ Rails::Rack::Logger,
71
+ Silencer::Logger,
72
+ config.log_tags,
73
+ get: [%r{^/assets/}],
74
+ post: [%r{^/some_path}]
75
+ )
76
+ ```
51
77
 
52
- ### Rails 2.3
78
+ Silencer's logger will serve as a drop-in replacement for Rails' default logger. It will not suppress any logging by default, simply pass it an array of URLs via the options hash. You can also send an `X-SILENCE-LOGGER` header (with any value) with your request and that will also produce the same behavior.
53
79
 
54
- Rails 2.3.x introduced a tagged logging feature. If you are using tagged logging with Rails 2.3 you can also pass taggers via the middleware:
80
+ ### All options
81
+
82
+ Silencer supports the following configuration options.
55
83
 
56
- config.middleware.swap Rails::Rack::Logger, Silencer::Logger, config.log_tags, :silence => [%r{^/assets/}]
84
+ | Configuration | Description | Default |
85
+ |---------------|-------------|---------|
86
+ | `silence` | Silences matching requests regardless of request method | None |
87
+ | `get` | Silences matching GET requests | None |
88
+ | `head` | Silences matching HEAD requests | None |
89
+ | `post` | Silences matching POST requests | None |
90
+ | `put` | Silences matching PUT requests | None |
91
+ | `delete` | Silences matching DELETE requests | None |
92
+ | `patch` | Silences matching PATCH requests | None |
93
+ | `trace` | Silences matching TRACE requests | None |
94
+ | `connect` | Silences matching CONNECT requests | None |
95
+ | `options` | Silences matching OPTIONS requests | None |
96
+ | `enable_header` | Enable/disable X-SILENCE-LOGGER header support | `true` |
57
97
 
58
98
  ## Note on Patches/Pull Requests
59
99
 
60
100
  * Fork the project.
61
- * Make your feature addition or bug fix.
101
+ * Make your feature addition or bugfix.
62
102
  * Add tests for it. This is important so I don't break it in a
63
103
  future version unintentionally.
64
104
  * Commit, do not mess with rakefile, version, or history.
@@ -67,4 +107,4 @@ Rails 2.3.x introduced a tagged logging feature. If you are using tagged loggin
67
107
 
68
108
  ## Copyright
69
109
 
70
- Copyright (c) 2012 Steve Agalloco. See [LICENSE](https://github.com/spagalloco/silencer/blob/master/LICENSE.md) for details.
110
+ Copyright (c) 2012 Steve Agalloco. See [LICENSE](https://github.com/spagalloco/silencer/blob/main/LICENSE.md) for details.
data/Rakefile CHANGED
@@ -1,21 +1,16 @@
1
- #!/usr/bin/env rake
2
- require "bundler/gem_tasks"
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
3
 
4
4
  require 'rspec/core/rake_task'
5
5
  RSpec::Core::RakeTask.new(:spec)
6
6
 
7
7
  task :default => :spec
8
- task :test => :spec
8
+ task test: :spec
9
9
 
10
- namespace :doc do
11
- require 'yard'
12
- YARD::Rake::YardocTask.new do |task|
13
- task.files = ['lib/**/*.rb']
14
- task.options = [
15
- '--protected',
16
- '--output-dir', 'doc/yard',
17
- '--markup', 'markdown',
18
- '--readme', 'README.md'
19
- ]
20
- end
21
- end
10
+ require 'rubocop/rake_task'
11
+ RuboCop::RakeTask.new
12
+
13
+ require 'yard'
14
+ YARD::Rake::YardocTask.new
15
+
16
+ task default: [:spec, :rubocop]
data/lib/silencer/hush.rb CHANGED
@@ -1,10 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Silencer
2
4
  module Hush
3
-
4
5
  private
5
6
 
6
- def silence_request?(env)
7
- (silent_header?(env) || silent_path?(env))
7
+ def silence_request?(env, enable_header: true)
8
+ ((enable_header && silent_header?(env)) || silent_path?(env))
8
9
  end
9
10
 
10
11
  def silent_header?(env)
@@ -12,8 +13,16 @@ module Silencer
12
13
  end
13
14
 
14
15
  def silent_path?(env)
15
- (@routes[env['REQUEST_METHOD']] || @silence).any? { |s| s === env['PATH_INFO'] }
16
+ (@routes[env['REQUEST_METHOD']] || @silence).any? do |rule|
17
+ case rule
18
+ when String, Integer
19
+ rule.to_s == env['PATH_INFO']
20
+ when Regexp
21
+ rule =~ env['PATH_INFO']
22
+ else
23
+ false
24
+ end
25
+ end
16
26
  end
17
-
18
27
  end
19
28
  end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Silencer
4
+ module Methods
5
+ METHODS = %i[options get head post put delete trace connect patch]
6
+
7
+ def define_routes(silence_paths, opts)
8
+ METHODS.each_with_object({}) do |method, routes|
9
+ routes[method.to_s.upcase] = wrap(opts.delete(method)) + silence_paths
10
+ routes
11
+ end
12
+ end
13
+ end
14
+ end
@@ -1,38 +1,51 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'rack/logger'
2
- require 'silencer/util'
3
4
  require 'silencer/hush'
5
+ require 'silencer/methods'
6
+ require 'silencer/util'
4
7
 
5
8
  module Silencer
6
9
  module Rack
7
10
  class Logger < ::Rack::Logger
8
- include Silencer::Util
9
11
  include Silencer::Hush
12
+ include Silencer::Methods
13
+ include Silencer::Util
10
14
 
11
15
  def initialize(app, *args)
12
16
  opts = extract_options!(args)
13
17
  @silence = wrap(opts.delete(:silence))
14
- @routes = {
15
- 'OPTIONS' => wrap(opts.delete(:options)) + @silence,
16
- 'GET' => wrap(opts.delete(:get)) + @silence,
17
- 'HEAD' => wrap(opts.delete(:head)) + @silence,
18
- 'POST' => wrap(opts.delete(:post)) + @silence,
19
- 'PUT' => wrap(opts.delete(:put)) + @silence,
20
- 'DELETE' => wrap(opts.delete(:delete)) + @silence,
21
- 'TRACE' => wrap(opts.delete(:trace)) + @silence,
22
- 'CONNECT' => wrap(opts.delete(:connect)) + @silence,
23
- 'PATCH' => wrap(opts.delete(:patch)) + @silence,
24
- }
25
-
26
- super app, *args
18
+ @routes = define_routes(@silence, opts)
19
+
20
+ @enable_header = opts.delete(:enable_header) { true }
21
+
22
+ super(app, *args)
27
23
  end
28
24
 
29
25
  def call(env)
30
- logger = ::Logger.new(env['rack.errors'])
31
- logger.level = (silence_request?(env) ? ::Logger::ERROR : @level)
26
+ if silence_request?(env, enable_header: @enable_header)
27
+ quiet(env) do
28
+ super
29
+ end
30
+ else
31
+ super
32
+ end
33
+ end
34
+
35
+ private
36
+
37
+ def quiet(env)
38
+ old_logger = env['rack.logger']
39
+ logger = ::Logger.new(env['rack.errors'])
40
+ logger.level = ::Logger::ERROR
32
41
 
33
42
  env['rack.logger'] = logger
34
- @app.call(env)
43
+ yield
44
+ ensure
45
+ env['rack.logger'] = old_logger
35
46
  end
36
47
  end
37
48
  end
49
+
50
+ Logger = Silencer::Rack::Logger
38
51
  end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Silencer
4
+ module Environment
5
+ RAILS_4 = /^4/
6
+ RAILS_5 = /^5/
7
+
8
+ module_function
9
+
10
+ def rails?
11
+ defined?(::Rails)
12
+ end
13
+
14
+ def rails_version
15
+ return unless rails?
16
+
17
+ ::Rails::VERSION::STRING
18
+ end
19
+
20
+ def rails4?
21
+ rails_version =~ RAILS_4
22
+ end
23
+
24
+ def rails5?
25
+ rails_version =~ RAILS_5
26
+ end
27
+
28
+ def tagged_logger?
29
+ rails4? || rails5?
30
+ end
31
+ end
32
+ end
@@ -1,33 +1,27 @@
1
- module Silencer
2
- RailsLogger = if Silencer::Environment.rails2?
3
- require 'rails/rack/log_tailer'
4
- ::Rails::Rack::LogTailer
5
- else
6
- require 'rails/rack/logger'
7
- ::Rails::Rack::Logger
8
- end
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails/rack/logger'
4
+ require 'silencer/hush'
5
+ require 'silencer/methods'
6
+ require 'silencer/util'
7
+ require 'silencer/rails/environment'
9
8
 
9
+ module Silencer
10
10
  module Rails
11
- class Logger < RailsLogger
12
- include Silencer::Util
11
+ class Logger < ::Rails::Rack::Logger
13
12
  include Silencer::Hush
13
+ include Silencer::Methods
14
+ include Silencer::Util
14
15
 
15
16
  def initialize(app, *args)
16
17
  opts = extract_options!(args)
17
18
  @silence = wrap(opts.delete(:silence))
18
- @routes = {
19
- 'OPTIONS' => wrap(opts.delete(:options)) + @silence,
20
- 'GET' => wrap(opts.delete(:get)) + @silence,
21
- 'HEAD' => wrap(opts.delete(:head)) + @silence,
22
- 'POST' => wrap(opts.delete(:post)) + @silence,
23
- 'PUT' => wrap(opts.delete(:put)) + @silence,
24
- 'DELETE' => wrap(opts.delete(:delete)) + @silence,
25
- 'TRACE' => wrap(opts.delete(:trace)) + @silence,
26
- 'CONNECT' => wrap(opts.delete(:connect)) + @silence,
27
- 'PATCH' => wrap(opts.delete(:patch)) + @silence,
28
- }
29
-
30
- if normalized_args = normalize(args)
19
+ @routes = define_routes(@silence, opts)
20
+
21
+ @enable_header = opts.delete(:enable_header) { true }
22
+
23
+ normalized_args = normalize(args)
24
+ if normalized_args
31
25
  super(app, normalized_args)
32
26
  else
33
27
  super(app)
@@ -35,19 +29,43 @@ module Silencer
35
29
  end
36
30
 
37
31
  def call(env)
32
+ if silence_request?(env, enable_header: @enable_header)
33
+ quiet do
34
+ super
35
+ end
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ private
42
+
43
+ def quiet(&block)
44
+ if ::Rails.logger.respond_to?(:silence) && ::Rails.logger.method(:silence).owner != ::Kernel
45
+ quiet_with_silence(&block)
46
+ else
47
+ quiet_with_log_level(&block)
48
+ end
49
+ end
50
+
51
+ # This is threadsafe in Rails 4.2.6+
52
+ def quiet_with_silence(&block)
53
+ ::Rails.logger.silence(&block)
54
+ end
55
+
56
+ # This is not threadsafe
57
+ def quiet_with_log_level
38
58
  old_logger_level = ::Rails.logger.level
39
- ::Rails.logger.level = ::Logger::ERROR if silence_request?(env)
59
+ ::Rails.logger.level = ::Logger::ERROR
40
60
 
41
- super
61
+ yield
42
62
  ensure
43
63
  # Return back to previous logging level
44
64
  ::Rails.logger.level = old_logger_level
45
65
  end
46
66
 
47
- private
48
-
49
67
  def normalize(args)
50
- args = case args.size
68
+ case args.size
51
69
  when 0 then nil
52
70
  when 1 then args.shift
53
71
  else args
@@ -55,4 +73,6 @@ module Silencer
55
73
  end
56
74
  end
57
75
  end
76
+
77
+ Logger = Silencer::Rails::Logger
58
78
  end
data/lib/silencer/util.rb CHANGED
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Silencer
2
4
  module Util
3
-
4
5
  def wrap(object)
5
6
  if object.nil?
6
7
  []
@@ -20,6 +21,5 @@ module Silencer
20
21
  end
21
22
 
22
23
  module_function :wrap, :extract_options!
23
-
24
24
  end
25
25
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Silencer
2
- VERSION = "1.0.0.rc3"
4
+ VERSION = '2.0.0'
3
5
  end
data/lib/silencer.rb CHANGED
@@ -1 +1 @@
1
- require 'silencer/logger'
1
+ # frozen_string_literal: true
data/silencer.gemspec CHANGED
@@ -14,7 +14,7 @@ Gem::Specification.new do |gem|
14
14
 
15
15
  gem.files = %w(.yardopts LICENSE.md README.md Rakefile silencer.gemspec)
16
16
  gem.files += Dir.glob("lib/**/*.rb")
17
- gem.files += Dir.glob("spec/**/*")
17
+
18
18
  gem.test_files = Dir.glob("spec/**/*")
19
19
 
20
20
  gem.require_paths = ["lib"]
@@ -1,63 +1,63 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
4
+ require 'silencer/rack/logger'
2
5
 
3
6
  describe Silencer::Rack::Logger do
4
- let(:app) { lambda { |env| [200, {}, ''] } }
7
+ let(:app) do
8
+ lambda { |env|
9
+ log = env['rack.logger']
10
+ log.info('ohai')
5
11
 
6
- it 'quiets the log when configured with a silenced path' do
7
- expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
12
+ [200, {'Content-Type' => 'text/plain'}, ['Hello, World!']]
13
+ }
14
+ end
8
15
 
9
- Silencer::Rack::Logger.new(app, :silence => ['/']).
10
- call(Rack::MockRequest.env_for("/"))
16
+ let(:errors) do
17
+ StringIO.new
11
18
  end
12
19
 
13
- it 'quiets the log when configured with a regex' do
14
- expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
20
+ it 'quiets the log when configured with a silenced path' do
21
+ a = Rack::Lint.new(Silencer::Rack::Logger.new(app, silence: ['/']))
22
+ Rack::MockRequest.new(a).get('/', 'rack.errors' => errors)
23
+ expect(errors).not_to match('ohai')
24
+ end
15
25
 
16
- Silencer::Rack::Logger.new(app, :silence => [/assets/]).
17
- call(Rack::MockRequest.env_for("/assets/application.css"))
26
+ it 'quiets the log when configured with a regex' do
27
+ a = Rack::Lint.new(Silencer::Rack::Logger.new(app, silence: [/assets/]))
28
+ Rack::MockRequest.new(a).get('/assets/application.css', 'rack.errors' => errors)
29
+ expect(errors).not_to match('ohai')
18
30
  end
19
31
 
20
- %w(OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT PATCH).each do |method|
32
+ %w[OPTIONS GET POST PUT DELETE PATCH].each do |method|
21
33
  it "quiets the log when configured with a silenced path for #{method} requests" do
22
- expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
23
-
24
- Silencer::Rack::Logger.new(app, method.downcase.to_sym => ['/']).
25
- call(Rack::MockRequest.env_for("/", :method => method))
34
+ a = Rack::Lint.new(Silencer::Rack::Logger.new(app, method.downcase.to_sym => ['/']))
35
+ Rack::MockRequest.new(a).send(method.downcase.to_sym, '/', 'rack.errors' => errors)
36
+ expect(errors).not_to match('ohai')
26
37
  end
27
38
 
28
39
  it "quiets the log when configured with a regex for #{method} requests" do
29
- expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
30
-
31
- Silencer::Rack::Logger.new(app, method.downcase.to_sym => [/assets/]).
32
- call(Rack::MockRequest.env_for("/assets/application.css", :method => method))
40
+ a = Rack::Lint.new(Silencer::Rack::Logger.new(app, method.downcase.to_sym => [/assets/]))
41
+ Rack::MockRequest.new(a).send(method.downcase.to_sym, '/assets/application.css', 'rack.errors' => errors)
42
+ expect(errors).not_to match('ohai')
33
43
  end
34
44
  end
35
45
 
36
- it 'quiets the log when configured with a silenced path for non-standard requests' do
37
- expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
38
-
39
- Silencer::Rack::Logger.new(app, :silence => ['/']).
40
- call(Rack::MockRequest.env_for("/", :method => 'UPDATE'))
41
- end
42
-
43
46
  it 'quiets the log when configured with a regex for non-standard requests' do
44
- expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
45
-
46
- Silencer::Rack::Logger.new(app, :silence => [/assets/]).
47
- call(Rack::MockRequest.env_for("/assets/application.css", :method => 'UPDATE'))
47
+ a = Rack::Lint.new(Silencer::Rack::Logger.new(app, silence: [/assets/]))
48
+ Rack::MockRequest.new(a).get('/', 'rack.errors' => errors)
49
+ expect(errors).not_to match('ohai')
48
50
  end
49
51
 
50
52
  it 'quiets the log when passed a custom header "X-SILENCE-LOGGER"' do
51
- expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
52
-
53
- Silencer::Rack::Logger.new(app).
54
- call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
53
+ a = Rack::Lint.new(Silencer::Rack::Logger.new(app))
54
+ Rack::MockRequest.new(a).get('/', 'rack.errors' => errors, 'HTTP_X_SILENCE_LOGGER' => 'true')
55
+ expect(errors).not_to match('ohai')
55
56
  end
56
57
 
57
58
  it 'does not tamper with the response' do
58
- response = Silencer::Rack::Logger.new(app).
59
- call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
60
-
61
- expect(response[0]).to eq(200)
59
+ a = Rack::Lint.new(Silencer::Rack::Logger.new(app))
60
+ response = Rack::MockRequest.new(a).get('/', 'rack.errors' => errors, 'HTTP_X_SILENCE_LOGGER' => 'true')
61
+ expect(response.status).to eq(200)
62
62
  end
63
63
  end
@@ -1,88 +1,111 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'spec_helper'
4
+ require 'syslogger'
5
+ require 'silencer/rails/logger'
2
6
 
3
7
  describe Silencer::Rails::Logger do
4
- let(:app) { lambda { |env| [200, {}, ''] } }
5
- let(:log_tags) { [:uuid, :queue] }
8
+ let(:app) { ->(_env) { [200, {}, ''] } }
9
+ let(:log_tags) { %i[uuid queue] }
6
10
 
7
- it 'quiets the log when configured with a silenced path' do
8
- expect(::Rails.logger).to receive(:level=).
9
- with(::Logger::ERROR).at_least(:once)
11
+ context 'quieted' do
12
+ before do
13
+ expect_any_instance_of(Silencer::Rails::Logger).to receive(:quiet).at_least(:once).and_call_original
14
+ end
10
15
 
11
- Silencer::Rails::Logger.new(app, :silence => ['/']).
12
- call(Rack::MockRequest.env_for("/"))
13
- end
16
+ it 'quiets the log when configured with a silenced path' do
17
+ Silencer::Rails::Logger.new(app, silence: ['/'])
18
+ .call(Rack::MockRequest.env_for('/'))
19
+ end
14
20
 
15
- it 'quiets the log when configured with a regex' do
16
- expect(::Rails.logger).to receive(:level=).
17
- with(::Logger::ERROR).at_least(:once)
21
+ it 'quiets the log when configured with a regex' do
22
+ Silencer::Rails::Logger.new(app, silence: [/assets/])
23
+ .call(Rack::MockRequest.env_for('/assets/application.css'))
24
+ end
18
25
 
19
- Silencer::Rails::Logger.new(app, :silence => [/assets/]).
20
- call(Rack::MockRequest.env_for("/assets/application.css"))
21
- end
26
+ %w[OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT PATCH].each do |method|
27
+ it "quiets the log when configured with a silenced path for #{method} requests" do
28
+ Silencer::Rails::Logger.new(app, method.downcase.to_sym => ['/'])
29
+ .call(Rack::MockRequest.env_for('/', method: method))
30
+ end
22
31
 
23
- %w(OPTIONS GET HEAD POST PUT DELETE TRACE CONNECT PATCH).each do |method|
24
- it "quiets the log when configured with a silenced path for #{method} requests" do
25
- expect(::Rails.logger).to receive(:level=).
26
- with(::Logger::ERROR).at_least(:once)
32
+ it "quiets the log when configured with a regex for #{method} requests" do
33
+ Silencer::Rails::Logger.new(app, method.downcase.to_sym => [/assets/])
34
+ .call(Rack::MockRequest.env_for('/assets/application.css', method: method))
35
+ end
36
+ end
27
37
 
28
- Silencer::Rails::Logger.new(app, method.downcase.to_sym => ['/']).
29
- call(Rack::MockRequest.env_for("/", :method => method))
38
+ it 'quiets the log when configured with a silenced path for non-standard requests' do
39
+ Silencer::Rails::Logger.new(app, silence: ['/'])
40
+ .call(Rack::MockRequest.env_for('/', method: 'UPDATE'))
30
41
  end
31
42
 
32
- it "quiets the log when configured with a regex for #{method} requests" do
33
- expect(::Rails.logger).to receive(:level=).
34
- with(::Logger::ERROR).at_least(:once)
43
+ it 'quiets the log when configured with a regex for non-standard requests' do
44
+ Silencer::Rails::Logger.new(app, silence: [/assets/])
45
+ .call(Rack::MockRequest.env_for('/assets/application.css', method: 'UPDATE'))
46
+ end
35
47
 
36
- Silencer::Rails::Logger.new(app, method.downcase.to_sym => [/assets/]).
37
- call(Rack::MockRequest.env_for("/assets/application.css", :method => method))
48
+ it 'quiets the log when passed a custom header "X-SILENCE-LOGGER"' do
49
+ Silencer::Rails::Logger.new(app)
50
+ .call(Rack::MockRequest.env_for('/', 'HTTP_X_SILENCE_LOGGER' => 'true'))
38
51
  end
39
- end
40
52
 
41
- it 'quiets the log when configured with a silenced path for non-standard requests' do
42
- expect(::Rails.logger).to receive(:level=).
43
- with(::Logger::ERROR).at_least(:once)
53
+ it 'does not tamper with the response' do
54
+ response = Silencer::Rails::Logger.new(app)
55
+ .call(Rack::MockRequest.env_for('/', 'HTTP_X_SILENCE_LOGGER' => 'true'))
44
56
 
45
- Silencer::Rails::Logger.new(app, :silence => ['/']).
46
- call(Rack::MockRequest.env_for("/", :method => 'UPDATE'))
47
- end
57
+ expect(response[0]).to eq(200)
58
+ end
48
59
 
49
- it 'quiets the log when configured with a regex for non-standard requests' do
50
- expect(::Rails.logger).to receive(:level=).
51
- with(::Logger::ERROR).at_least(:once)
60
+ if Silencer::Environment.tagged_logger?
61
+ it 'instantiates with an optional taggers array' do
62
+ Silencer::Rails::Logger.new(app, log_tags, silence: ['/'])
63
+ .call(Rack::MockRequest.env_for('/'))
64
+ end
52
65
 
53
- Silencer::Rails::Logger.new(app, :silence => [/assets/]).
54
- call(Rack::MockRequest.env_for("/assets/application.css", :method => 'UPDATE'))
66
+ it 'instantiates with an optional taggers array passed as args' do
67
+ Silencer::Rails::Logger.new(app, :uuid, :queue, silence: ['/'])
68
+ .call(Rack::MockRequest.env_for('/'))
69
+ end
70
+ end
55
71
  end
56
72
 
57
- it 'quiets the log when passed a custom header "X-SILENCE-LOGGER"' do
58
- expect(::Rails.logger).to receive(:level=).
59
- with(::Logger::ERROR).at_least(:once)
73
+ describe 'enable_header option' do
74
+ it 'does not quiet the log when passed a custom header "X-SILENCE-LOGGER" when enable_header option is false' do
75
+ expect_any_instance_of(Silencer::Rails::Logger).to_not receive(:quiet)
60
76
 
61
- Silencer::Rails::Logger.new(app).
62
- call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
63
- end
77
+ Silencer::Rails::Logger.new(app, enable_header: false)
78
+ .call(Rack::MockRequest.env_for('/', 'HTTP_X_SILENCE_LOGGER' => 'true'))
79
+ end
64
80
 
65
- it 'does not tamper with the response' do
66
- response = Silencer::Rails::Logger.new(app).
67
- call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
81
+ it 'quiets the log when passed a custom header "X-SILENCE-LOGGER" when enable_header option is true' do
82
+ expect_any_instance_of(Silencer::Rails::Logger).to receive(:quiet).once.and_call_original
68
83
 
69
- expect(response[0]).to eq(200)
84
+ Silencer::Rails::Logger.new(app, enable_header: true)
85
+ .call(Rack::MockRequest.env_for('/', 'HTTP_X_SILENCE_LOGGER' => 'true'))
86
+ end
70
87
  end
71
88
 
72
- it 'instantiates with an optional taggers array' do
73
- expect(::Rails.logger).to receive(:level=).
74
- with(::Logger::ERROR).at_least(:once)
89
+ it 'silences' do
90
+ logger = Silencer::Rails::Logger.new(app, silence: ['/'])
75
91
 
76
- Silencer::Rails::Logger.new(app, log_tags, :silence => ['/']).
77
- call(Rack::MockRequest.env_for("/"))
78
- end if Silencer::Environment.tagged_logger?
92
+ if ::Rails.logger.respond_to?(:silence)
93
+ expect(::Rails.logger).to receive(:silence).at_least(:once)
94
+ else
95
+ expect(::Rails.logger).to receive(:level=)
96
+ .with(::Logger::ERROR).at_least(:once)
97
+ end
79
98
 
80
- it 'instantiates with an optional taggers array passed as args' do
81
- expect(::Rails.logger).to receive(:level=).
82
- with(::Logger::ERROR).at_least(:once)
99
+ logger.call(Rack::MockRequest.env_for('/'))
100
+ end
83
101
 
84
- Silencer::Rails::Logger.new(app, :uuid, :queue, :silence => ['/']).
85
- call(Rack::MockRequest.env_for("/"))
86
- end if Silencer::Environment.tagged_logger?
102
+ context 'when logger is Syslogger' do
103
+ it 'does not throw error' do
104
+ mock_rails_logger = Syslogger.new
105
+ allow(::Rails).to receive(:logger) { mock_rails_logger }
87
106
 
107
+ logger = Silencer::Rails::Logger.new(app, silence: ['/'])
108
+ expect { logger.call(Rack::MockRequest.env_for('/')) }.not_to raise_error
109
+ end
110
+ end
88
111
  end
data/spec/spec_helper.rb CHANGED
@@ -1,13 +1,15 @@
1
+ # frozen_string_literal: true
2
+
1
3
  unless ENV['CI']
2
4
  require 'simplecov'
3
5
  SimpleCov.start do
4
- add_filter '.bundle'
5
6
  add_group 'Silencer', 'lib/silencer'
6
7
  add_group 'Specs', 'spec'
7
8
  end
8
9
  end
9
10
 
10
11
  require 'rack'
12
+ require 'rack/lint'
11
13
  require 'rspec'
12
14
 
13
15
  require 'logger'
@@ -17,7 +19,11 @@ io = StringIO.new
17
19
 
18
20
  begin
19
21
  require 'rails'
20
- ::Rails.logger = ::Logger.new(io)
22
+ ::Rails.logger = if Rails::VERSION::MAJOR >= 4
23
+ ::ActiveSupport::Logger.new(io)
24
+ else
25
+ ::Logger.new(io)
26
+ end
21
27
  rescue LoadError
22
28
  require 'activesupport'
23
29
  RAILS_ENV = 'test'
@@ -36,5 +42,4 @@ RSpec.configure do |config|
36
42
  config.before(:each) do
37
43
  allow(::Rails.logger).to receive(:level=).with(anything)
38
44
  end
39
-
40
45
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: silencer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.rc3
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Steve Agalloco
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-06-10 00:00:00.000000000 Z
11
+ date: 2022-04-24 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Selectively quiet your Rails/Rack logger on a per-route basis
14
14
  email:
@@ -22,10 +22,10 @@ files:
22
22
  - README.md
23
23
  - Rakefile
24
24
  - lib/silencer.rb
25
- - lib/silencer/environment.rb
26
25
  - lib/silencer/hush.rb
27
- - lib/silencer/logger.rb
26
+ - lib/silencer/methods.rb
28
27
  - lib/silencer/rack/logger.rb
28
+ - lib/silencer/rails/environment.rb
29
29
  - lib/silencer/rails/logger.rb
30
30
  - lib/silencer/util.rb
31
31
  - lib/silencer/version.rb
@@ -37,7 +37,7 @@ files:
37
37
  homepage: http://github.com/spagalloco/silencer
38
38
  licenses: []
39
39
  metadata: {}
40
- post_install_message:
40
+ post_install_message:
41
41
  rdoc_options: []
42
42
  require_paths:
43
43
  - lib
@@ -48,13 +48,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
48
48
  version: '0'
49
49
  required_rubygems_version: !ruby/object:Gem::Requirement
50
50
  requirements:
51
- - - ">"
51
+ - - ">="
52
52
  - !ruby/object:Gem::Version
53
- version: 1.3.1
53
+ version: '0'
54
54
  requirements: []
55
- rubyforge_project:
56
- rubygems_version: 2.2.2
57
- signing_key:
55
+ rubygems_version: 3.3.7
56
+ signing_key:
58
57
  specification_version: 4
59
58
  summary: Selectively quiet your Rails/Rack logger on a per-route basis
60
59
  test_files:
@@ -62,4 +61,3 @@ test_files:
62
61
  - spec/silencer/rack/logger_spec.rb
63
62
  - spec/silencer/rails/logger_spec.rb
64
63
  - spec/spec_helper.rb
65
- has_rdoc:
@@ -1,37 +0,0 @@
1
- module Silencer
2
- module Environment
3
-
4
- RAILS_2_3 = %r{^2.3}
5
- RAILS_3_2 = %r{^3.2}
6
- RAILS_4 = %r{^4}
7
-
8
- def rails?
9
- defined?(::Rails)
10
- end
11
-
12
- def rails_version
13
- return unless rails?
14
- ::Rails::VERSION::STRING
15
- end
16
-
17
- def rails2?
18
- rails_version =~ RAILS_2_3
19
- end
20
-
21
- def rails3_2?
22
- rails_version =~ RAILS_3_2
23
- end
24
-
25
- def rails4?
26
- rails_version =~ RAILS_4
27
- end
28
-
29
- def tagged_logger?
30
- rails3_2? || rails4?
31
- end
32
-
33
- module_function :rails?, :rails2?, :rails_version, :rails3_2?
34
- module_function :rails4?, :tagged_logger?
35
-
36
- end
37
- end
@@ -1,11 +0,0 @@
1
- require 'silencer/environment'
2
- require 'silencer/rack/logger'
3
- require 'silencer/rails/logger' if Silencer::Environment.rails?
4
-
5
- module Silencer
6
- Logger = if Silencer::Environment.rails?
7
- Silencer::Rails::Logger
8
- else
9
- Silencer::Rack::Logger
10
- end
11
- end