silencer 0.6.0 → 1.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -5,6 +5,8 @@
5
5
 
6
6
  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
7
 
8
+ __Note__: Silencer is not thread safe.
9
+
8
10
  ## Installation
9
11
 
10
12
  Just add silencer to your Gemfile:
@@ -0,0 +1,37 @@
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
@@ -0,0 +1,19 @@
1
+ module Silencer
2
+ module Hush
3
+
4
+ private
5
+
6
+ def silence_request?(env)
7
+ (silent_header?(env) || silent_path?(env))
8
+ end
9
+
10
+ def silent_header?(env)
11
+ env['HTTP_X_SILENCE_LOGGER']
12
+ end
13
+
14
+ def silent_path?(env)
15
+ @silence.any? { |s| s === env['PATH_INFO'] }
16
+ end
17
+
18
+ end
19
+ end
@@ -1,29 +1,11 @@
1
- require 'rails/rack/logger'
2
- require 'active_support/core_ext/array/wrap'
1
+ require 'silencer/environment'
2
+ require 'silencer/rack/logger'
3
+ require 'silencer/rails/logger' if Silencer::Environment.rails?
3
4
 
4
5
  module Silencer
5
- class Logger < Rails::Rack::Logger
6
- def initialize(app, *taggers)
7
- @app = app
8
- opts = taggers.extract_options!
9
- @taggers = taggers.flatten
10
- @silence = Array.wrap(opts[:silence])
11
- end
12
-
13
- def call(env)
14
- old_logger_level = Rails.logger.level
15
- Rails.logger.level = ::Logger::ERROR if silence_request?(env)
16
-
17
- super
18
- ensure
19
- # Return back to previous logging level
20
- Rails.logger.level = old_logger_level
21
- end
22
-
23
- private
24
-
25
- def silence_request?(env)
26
- env['HTTP_X_SILENCE_LOGGER'] || @silence.any? { |s| s === env['PATH_INFO'] }
27
- end
6
+ Logger = if Silencer::Environment.rails?
7
+ Silencer::Rails::Logger
8
+ else
9
+ Silencer::Rack::Logger
28
10
  end
29
11
  end
@@ -0,0 +1,27 @@
1
+ require 'rack/logger'
2
+ require 'silencer/util'
3
+ require 'silencer/hush'
4
+
5
+ module Silencer
6
+ module Rack
7
+ class Logger < ::Rack::Logger
8
+ include Silencer::Util
9
+ include Silencer::Hush
10
+
11
+ def initialize(app, *args)
12
+ opts = extract_options!(args)
13
+ @silence = wrap(opts.delete(:silence))
14
+
15
+ super app, *args
16
+ end
17
+
18
+ def call(env)
19
+ logger = ::Logger.new(env['rack.errors'])
20
+ logger.level = (silence_request?(env) ? ::Logger::ERROR : @level)
21
+
22
+ env['rack.logger'] = logger
23
+ @app.call(env)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,47 @@
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
9
+
10
+ module Rails
11
+ class Logger < RailsLogger
12
+ include Silencer::Util
13
+ include Silencer::Hush
14
+
15
+ def initialize(app, *args)
16
+ opts = extract_options!(args)
17
+ @silence = wrap(opts.delete(:silence))
18
+
19
+ if normalized_args = normalize(args.flatten)
20
+ super(app, normalized_args)
21
+ else
22
+ super(app)
23
+ end
24
+ end
25
+
26
+ def call(env)
27
+ old_logger_level = ::Rails.logger.level
28
+ ::Rails.logger.level = ::Logger::ERROR if silence_request?(env)
29
+
30
+ super
31
+ ensure
32
+ # Return back to previous logging level
33
+ ::Rails.logger.level = old_logger_level
34
+ end
35
+
36
+ private
37
+
38
+ def normalize(args)
39
+ args = case args.size
40
+ when 0 then nil
41
+ when 1 then args.shift
42
+ else args
43
+ end
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,25 @@
1
+ module Silencer
2
+ module Util
3
+
4
+ def wrap(object)
5
+ if object.nil?
6
+ []
7
+ elsif object.respond_to?(:to_ary)
8
+ object.to_ary || [object]
9
+ else
10
+ [object]
11
+ end
12
+ end
13
+
14
+ def extract_options!(args)
15
+ if args.last.is_a?(Hash)
16
+ args.pop
17
+ else
18
+ {}
19
+ end
20
+ end
21
+
22
+ module_function :wrap, :extract_options!
23
+
24
+ end
25
+ end
@@ -1,3 +1,3 @@
1
1
  module Silencer
2
- VERSION = "0.6.0"
2
+ VERSION = "1.0.0.rc1"
3
3
  end
@@ -1,5 +1,7 @@
1
- # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/silencer/version', __FILE__)
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'silencer/version'
3
5
 
4
6
  Gem::Specification.new do |gem|
5
7
  gem.name = "silencer"
@@ -7,13 +9,13 @@ Gem::Specification.new do |gem|
7
9
  gem.authors = ["Steve Agalloco"]
8
10
  gem.email = ["steve.agalloco@gmail.com"]
9
11
  gem.homepage = "http://github.com/spagalloco/silencer"
10
- gem.description = 'Selectively quiet your Rails logger on a per-action basis'
12
+ gem.description = 'Selectively quiet your Rails/Rack logger on a per-route basis'
11
13
  gem.summary = gem.description
12
14
 
13
- gem.add_dependency 'railties', '>= 3'
15
+ gem.files = %w(.yardopts LICENSE.md README.md Rakefile silencer.gemspec)
16
+ gem.files += Dir.glob("lib/**/*.rb")
17
+ gem.files += Dir.glob("spec/**/*")
18
+ gem.test_files = Dir.glob("spec/**/*")
14
19
 
15
- gem.files = `git ls-files`.split("\n")
16
- gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
17
- gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
18
20
  gem.require_paths = ["lib"]
19
21
  end
File without changes
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Silencer::Rack::Logger do
4
+ let(:app) { lambda { |env| [200, {}, ''] } }
5
+
6
+ it 'quiets the log when configured with a silenced path' do
7
+ expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
8
+
9
+ Silencer::Rack::Logger.new(app, :silence => ['/']).
10
+ call(Rack::MockRequest.env_for("/"))
11
+ end
12
+
13
+ it 'quiets the log when configured with a regex' do
14
+ expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
15
+
16
+ Silencer::Rack::Logger.new(app, :silence => [/assets/]).
17
+ call(Rack::MockRequest.env_for("/assets/application.css"))
18
+ end
19
+
20
+ it 'quiets the log when passed a custom header "X-SILENCE-LOGGER"' do
21
+ expect_any_instance_of(::Logger).to receive(:level=).with(::Logger::ERROR)
22
+
23
+ Silencer::Rack::Logger.new(app).
24
+ call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
25
+ end
26
+
27
+ it 'does not tamper with the response' do
28
+ response = Silencer::Rack::Logger.new(app).
29
+ call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
30
+
31
+ expect(response[0]).to eq(200)
32
+ end
33
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Silencer::Rails::Logger do
4
+ let(:app) { lambda { |env| [200, {}, ''] } }
5
+ let(:log_tags) { [:uuid, :queue] }
6
+
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)
10
+
11
+ Silencer::Rails::Logger.new(app, :silence => ['/']).
12
+ call(Rack::MockRequest.env_for("/"))
13
+ end
14
+
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)
18
+
19
+ Silencer::Rails::Logger.new(app, :silence => [/assets/]).
20
+ call(Rack::MockRequest.env_for("/assets/application.css"))
21
+ end
22
+
23
+ it 'quiets the log when passed a custom header "X-SILENCE-LOGGER"' do
24
+ expect(::Rails.logger).to receive(:level=).
25
+ with(::Logger::ERROR).at_least(:once)
26
+
27
+ Silencer::Rails::Logger.new(app).
28
+ call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
29
+ end
30
+
31
+ it 'does not tamper with the response' do
32
+ response = Silencer::Rails::Logger.new(app).
33
+ call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
34
+
35
+ expect(response[0]).to eq(200)
36
+ end
37
+
38
+ it 'instantiates with an optional taggers array' do
39
+ expect(::Rails.logger).to receive(:level=).
40
+ with(::Logger::ERROR).at_least(:once)
41
+
42
+ Silencer::Rails::Logger.new(app, log_tags, :silence => ['/']).
43
+ call(Rack::MockRequest.env_for("/"))
44
+ end if Silencer::Environment.tagged_logger?
45
+
46
+ it 'instantiates with an optional taggers array passed as args' do
47
+ expect(::Rails.logger).to receive(:level=).
48
+ with(::Logger::ERROR).at_least(:once)
49
+
50
+ Silencer::Rails::Logger.new(app, :uuid, :queue, :silence => ['/']).
51
+ call(Rack::MockRequest.env_for("/"))
52
+ end if Silencer::Environment.tagged_logger?
53
+
54
+ end
@@ -8,6 +8,33 @@ unless ENV['CI']
8
8
  end
9
9
 
10
10
  require 'rack'
11
- require 'rails'
12
- require 'silencer'
13
11
  require 'rspec'
12
+
13
+ require 'logger'
14
+ require 'stringio'
15
+
16
+ io = StringIO.new
17
+
18
+ begin
19
+ require 'rails'
20
+ ::Rails.logger = ::Logger.new(io)
21
+ rescue LoadError
22
+ require 'activesupport'
23
+ RAILS_ENV = 'test'
24
+ RAILS_ROOT = File.dirname(__FILE__)
25
+ RAILS_DEFAULT_LOGGER = ::Logger.new(io)
26
+ require 'initializer'
27
+ end
28
+
29
+ require 'silencer'
30
+
31
+ RSpec.configure do |config|
32
+ config.expect_with :rspec do |c|
33
+ c.syntax = :expect
34
+ end
35
+
36
+ config.before(:each) do
37
+ allow(::Rails.logger).to receive(:level=).with(anything)
38
+ end
39
+
40
+ end
metadata CHANGED
@@ -1,54 +1,39 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: silencer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
5
- prerelease:
4
+ version: 1.0.0.rc1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Steve Agalloco
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-06-02 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: railties
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '3'
22
- type: :runtime
23
- prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '3'
30
- description: Selectively quiet your Rails logger on a per-action basis
12
+ date: 2013-10-20 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description: Selectively quiet your Rails/Rack logger on a per-route basis
31
15
  email:
32
16
  - steve.agalloco@gmail.com
33
17
  executables: []
34
18
  extensions: []
35
19
  extra_rdoc_files: []
36
20
  files:
37
- - .gemtest
38
- - .gitignore
39
- - .rspec
40
- - .travis.yml
41
21
  - .yardopts
42
- - Gemfile
43
- - Guardfile
44
22
  - LICENSE.md
45
23
  - README.md
46
24
  - Rakefile
47
- - lib/silencer.rb
25
+ - silencer.gemspec
26
+ - lib/silencer/environment.rb
27
+ - lib/silencer/hush.rb
48
28
  - lib/silencer/logger.rb
29
+ - lib/silencer/rack/logger.rb
30
+ - lib/silencer/rails/logger.rb
31
+ - lib/silencer/util.rb
49
32
  - lib/silencer/version.rb
50
- - silencer.gemspec
51
- - spec/silencer_spec.rb
33
+ - lib/silencer.rb
34
+ - spec/log/test.log
35
+ - spec/silencer/rack/logger_spec.rb
36
+ - spec/silencer/rails/logger_spec.rb
52
37
  - spec/spec_helper.rb
53
38
  homepage: http://github.com/spagalloco/silencer
54
39
  licenses: []
@@ -64,23 +49,22 @@ required_ruby_version: !ruby/object:Gem::Requirement
64
49
  version: '0'
65
50
  segments:
66
51
  - 0
67
- hash: -4361503101731160331
52
+ hash: 3624886497730780016
68
53
  required_rubygems_version: !ruby/object:Gem::Requirement
69
54
  none: false
70
55
  requirements:
71
- - - ! '>='
56
+ - - ! '>'
72
57
  - !ruby/object:Gem::Version
73
- version: '0'
74
- segments:
75
- - 0
76
- hash: -4361503101731160331
58
+ version: 1.3.1
77
59
  requirements: []
78
60
  rubyforge_project:
79
61
  rubygems_version: 1.8.23
80
62
  signing_key:
81
63
  specification_version: 3
82
- summary: Selectively quiet your Rails logger on a per-action basis
64
+ summary: Selectively quiet your Rails/Rack logger on a per-route basis
83
65
  test_files:
84
- - spec/silencer_spec.rb
66
+ - spec/log/test.log
67
+ - spec/silencer/rack/logger_spec.rb
68
+ - spec/silencer/rails/logger_spec.rb
85
69
  - spec/spec_helper.rb
86
70
  has_rdoc:
data/.gitignore DELETED
@@ -1,17 +0,0 @@
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/*.log
data/.rspec DELETED
@@ -1,3 +0,0 @@
1
- --color
2
- --format=nested
3
- --backtrace
@@ -1,14 +0,0 @@
1
- bundler_args: --without development
2
- language: ruby
3
- rvm:
4
- - jruby-18mode
5
- - jruby-19mode
6
- - rbx-18mode
7
- - rbx-19mode
8
- - 1.8.7
9
- - 1.9.2
10
- - 1.9.3
11
- - 2.0.0
12
- matrix:
13
- allow_failures:
14
- - rvm: 2.0.0
data/Gemfile DELETED
@@ -1,16 +0,0 @@
1
- source 'http://rubygems.org'
2
-
3
- gem 'rake'
4
- gem 'yard'
5
-
6
- group :development do
7
- gem 'kramdown'
8
- gem 'guard-rspec'
9
- end
10
-
11
- group :test do
12
- gem 'rspec'
13
- gem 'simplecov', :require => false
14
- end
15
-
16
- gemspec
data/Guardfile DELETED
@@ -1,21 +0,0 @@
1
- # A sample Guardfile
2
- # More info at https://github.com/guard/guard#readme
3
-
4
- guard 'rspec', :version => 2 do
5
- watch(%r{^spec/.+_spec\.rb$})
6
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
7
- watch('spec/spec_helper.rb') { "spec" }
8
-
9
- # Rails example
10
- watch(%r{^spec/.+_spec\.rb$})
11
- watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
12
- watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
13
- watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
14
- watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
15
- watch('spec/spec_helper.rb') { "spec" }
16
- watch('config/routes.rb') { "spec/routing" }
17
- watch('app/controllers/application_controller.rb') { "spec/controllers" }
18
- # Capybara request specs
19
- watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
20
- end
21
-
@@ -1,63 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Silencer::Logger do
4
- before(:each) do
5
- @app = lambda { |env| [200, {}, ''] }
6
-
7
- Rails.logger = stub('ActiveSupport::BufferedLogger').tap do |l|
8
- l.stub(:level).and_return(::Logger::INFO)
9
- l.stub(:level=).and_return(true)
10
- l.stub(:info)
11
- end
12
- end
13
-
14
- def should_silence_logger
15
- Rails.logger.should_receive(:level=).with(::Logger::ERROR).once
16
- end
17
- def should_not_silence_logger
18
- Rails.logger.should_not_receive(:level=).with(::Logger::ERROR)
19
- end
20
-
21
- it 'by default does not silence any requests or use any taggers' do
22
- should_not_silence_logger
23
- logger = Silencer::Logger.new(@app)
24
- logger.instance_variable_get(:@taggers).should == []
25
- logger.call(Rack::MockRequest.env_for("/"))
26
- end
27
-
28
- let(:log_tags) { [:uuid, :queue] }
29
- it 'instantiates with an optional taggers array' do
30
- should_silence_logger
31
- logger = Silencer::Logger.new(@app, log_tags, :silence => ['/'])
32
- logger.instance_variable_get(:@taggers).should == log_tags
33
- logger.call(Rack::MockRequest.env_for("/"))
34
- end
35
-
36
- # Note: initially this test used a splat on log_tags but it fails on 1.8
37
- it 'instantiates with an optional taggers array passed as args' do
38
- should_silence_logger
39
- logger = Silencer::Logger.new(@app, :uuid, :queue, :silence => ['/'])
40
- logger.instance_variable_get(:@taggers).should == log_tags
41
- logger.call(Rack::MockRequest.env_for("/"))
42
- end
43
-
44
- it 'quiets the log when configured with a silenced path' do
45
- should_silence_logger
46
- Silencer::Logger.new(@app, :silence => ['/']).call(Rack::MockRequest.env_for("/"))
47
- end
48
-
49
- it 'quiets the log when configured with a regex' do
50
- should_silence_logger
51
- Silencer::Logger.new(@app, :silence => [/assets/]).call(Rack::MockRequest.env_for("/assets/application.css"))
52
- end
53
-
54
- it 'quiets the log when passed a custom header "X-SILENCE-LOGGER"' do
55
- should_silence_logger
56
- Silencer::Logger.new(@app).call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
57
- end
58
-
59
- it 'does not tamper with the response' do
60
- response = Silencer::Logger.new(@app).call(Rack::MockRequest.env_for("/", 'HTTP_X_SILENCE_LOGGER' => 'true'))
61
- response[0].should eq(200)
62
- end
63
- end