muffler 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm 1.9.3@muffler --create
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ branches:
2
+ only:
3
+ - master
4
+ language: ruby
5
+ rvm:
6
+ - 1.9.2
7
+ - 1.9.3
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in muffler.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,5 @@
1
+ guard 'minitest' do
2
+ watch(%r|^spec/(.*)_spec\.rb|)
3
+ watch(%r|^lib/(.*)([^/]+)\.rb|) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
4
+ watch(%r|^spec/spec_helper\.rb|) { "spec" }
5
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Jeremy Israelsen
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,150 @@
1
+ Muffler [![Build Status](https://secure.travis-ci.org/jisraelsen/muffler.png?branch=master)](http://travis-ci.org/jisraelsen/muffler)
2
+ ========
3
+
4
+ Flexible log suppression for Ruby. Compatible with:
5
+
6
+ * Rails 3.x
7
+ * Rack
8
+ * Ruby 1.9.x
9
+
10
+ Install
11
+ -------
12
+
13
+ Just add it to your Gemfile:
14
+
15
+ ```ruby
16
+ gem 'muffler'
17
+ ```
18
+
19
+ Usage
20
+ -----
21
+
22
+ ### Rails
23
+
24
+ Works as drop-in replacement for `Rails::Rack::Logger`. Gemfile require
25
+ is all that's needed.
26
+
27
+ ### Rack
28
+
29
+ Drop-in replacements are provided for `Rack::CommonLogger` and
30
+ `Rack::Logger`. Just add them to your middleware stack.
31
+
32
+ ```ruby
33
+ use Muffler::Rack::CommonLogger
34
+ use Muffler::Rack::Logger
35
+
36
+ run MyApp
37
+ ```
38
+
39
+ ### Ruby
40
+
41
+ If not using Rack or Rails you can still muffle log output by manually
42
+ calling the muffle method:
43
+
44
+ ```ruby
45
+ logger = Logger.new('app.log')
46
+
47
+ Muffler.muffle(logger) do
48
+ # your custom code here
49
+ end
50
+ ```
51
+
52
+ You can still take advantage of conditional muffling as well:
53
+
54
+ ```ruby
55
+ logger = Logger.new('app.log')
56
+
57
+ if Muffler.muffle?('REMOTE_ADDR' => '127.0.0.1')
58
+ Muffler.muffle(logger) do
59
+ # muffled code here
60
+ end
61
+ else
62
+ # unmuffled code
63
+ end
64
+ ```
65
+
66
+ By default, Muffler muffles log output based on the request environment.
67
+ It does comparisons against `REMOTE_ADDR`, `PATH_INFO`, and a custom
68
+ header you can configure. The default header looked for is:
69
+ `HTTP_X_MUFFLE_LOGGER`.
70
+
71
+ Note that when sending the abover header in the request it should be
72
+ named `X-Muffle-Logger` or `X-MUFFLE-LOGGER`.
73
+
74
+ You can configure which IP addresses, request paths, and headers to look
75
+ for:
76
+
77
+ ```ruby
78
+ Muffler.configure do |config|
79
+ config.ips += ['10.1.1.3', '10.1.1.4']
80
+ config.paths << %r{/assets}
81
+ config.header = 'HTTP_X_MY_CUSTOM_HEADER'
82
+ end
83
+ ```
84
+
85
+ You can also add your own "mufflers" or replace the defaults. Custom
86
+ mufflers just need to return true (muffle log output) or false (don't
87
+ muffle log output).
88
+
89
+ If still using Rack or Rails, you could add a params muffler:
90
+
91
+ ```ruby
92
+ Muffler.configure do |config|
93
+ config.muffle_with :params do |env|
94
+ request = Rack::Request.new(env)
95
+ request.params['muffle'].to_i == 1
96
+ end
97
+ end
98
+ ```
99
+
100
+ Of if just working with Ruby you could clear the default filters and add
101
+ your own:
102
+
103
+ ```ruby
104
+ Muffler.configure do |config|
105
+ config.mufflers.clear # clear default mufflers
106
+ config.muffle_with :random do |opts|
107
+ [true, false].sample
108
+ end
109
+ end
110
+ ```
111
+
112
+ The default mufflers are:
113
+
114
+ * `:header`
115
+ * `:ip`
116
+ * `:path`
117
+
118
+ Contributing
119
+ ------------
120
+
121
+ Pull requests are welcome. Just make sure to include tests!
122
+
123
+ To run tests, install some dependencies:
124
+
125
+ ```bash
126
+ bundle install
127
+ ```
128
+
129
+ Then, run tests with:
130
+
131
+ ```bash
132
+ rake test
133
+ ```
134
+
135
+ Or, If you want to check coverage:
136
+
137
+ ```bash
138
+ COVERAGE=true rake test
139
+ ```
140
+
141
+ Issues
142
+ ------
143
+
144
+ Please use GitHub's [issue tracker](http://github.com/jisraelsen/muffler/issues).
145
+
146
+ Author
147
+ ------
148
+
149
+ [Jeremy Israelsen](http://github.com/jisraelsen)
150
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler/gem_tasks'
2
+ require 'rake/testtask'
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs.push 'spec'
6
+ t.test_files = FileList['spec/**/*_spec.rb']
7
+ t.verbose = true
8
+ end
9
+
10
+ task :default => :test
@@ -0,0 +1,20 @@
1
+ module Muffler
2
+ class Config
3
+ attr_accessor :header, :ips, :paths, :mufflers
4
+
5
+ def initialize
6
+ @header = 'HTTP_X_MUFFLE_LOGGER' # send in header as X-Muffle-Logger
7
+ @ips = []
8
+ @paths = []
9
+ @mufflers = {}
10
+
11
+ muffle_with(:header) { |opts| !opts[@header].nil? }
12
+ muffle_with(:ip) { |opts| @ips.any? { |ip| ip === opts['REMOTE_ADDR'] } }
13
+ muffle_with(:path) { |opts| @paths.any? { |path| path === opts['PATH_INFO'] } }
14
+ end
15
+
16
+ def muffle_with(name, &block)
17
+ @mufflers[name] = block
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ module Muffler
2
+ module Rack
3
+ class CommonLogger < ::Rack::CommonLogger
4
+ private
5
+ def log(env, status, header, began_at)
6
+ super unless Muffler.muffle?(env)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,18 @@
1
+ module Muffler
2
+ module Rack
3
+ class Logger < ::Rack::Logger
4
+ def call(env)
5
+ logger = ::Logger.new(env['rack.errors'])
6
+ logger.level = @level
7
+
8
+ env['rack.logger'] = logger
9
+
10
+ if Muffler.muffle?(env)
11
+ Muffler.muffle(logger) { @app.call(env) }
12
+ else
13
+ @app.call(env)
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,2 @@
1
+ require 'muffler/rack/commonlogger'
2
+ require 'muffler/rack/logger'
@@ -0,0 +1,15 @@
1
+ require 'rails/rack/logger'
2
+
3
+ module Muffler
4
+ module Rails
5
+ class Logger < ::Rails::Rack::Logger
6
+ def call(env)
7
+ if Muffler.muffle?(env)
8
+ Muffler.muffle(::Rails.logger) { super }
9
+ else
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,11 @@
1
+ require 'muffler/rails/logger'
2
+
3
+ module Muffler
4
+ module Rails
5
+ class Railtie < ::Rails::Railtie
6
+ initializer 'muffler.middleware' do |app|
7
+ app.config.middleware.swap ::Rails::Rack::Logger, Muffler::Rails::Logger, app.config.log_tags
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Muffler
2
+ VERSION = "0.0.1"
3
+ end
data/lib/muffler.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'logger'
2
+ require 'muffler/config'
3
+ require 'muffler/version'
4
+
5
+ if defined?(Rails)
6
+ require 'muffler/rails'
7
+ elsif defined?(Rack)
8
+ require 'muffler/rack'
9
+ end
10
+
11
+ module Muffler
12
+ class << self
13
+ def muffle?(opts={})
14
+ config.mufflers.values.any? { |m| m.call(opts) }
15
+ end
16
+
17
+ def muffle(logger, level=Logger::ERROR)
18
+ original_level = logger.level
19
+ logger.level = level
20
+
21
+ yield
22
+ ensure
23
+ logger.level = original_level
24
+ end
25
+
26
+ def configure
27
+ yield config
28
+ end
29
+
30
+ def config
31
+ @config ||= Config.new
32
+ end
33
+ end
34
+ end
data/muffler.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'muffler/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "muffler"
8
+ gem.version = Muffler::VERSION
9
+ gem.authors = ["Jeremy Israelsen"]
10
+ gem.email = ["jisraelsen@gmail.com"]
11
+ gem.description = %q{Flexible log suppression for Ruby, Rack, and Rails.}
12
+ gem.summary = %q{Flexible log suppression library.}
13
+ gem.homepage = "http://github.com/jisraelsen/muffler"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ gem.required_ruby_version = '>= 1.9.2'
21
+
22
+ gem.add_development_dependency 'rake'
23
+ gem.add_development_dependency 'bundler', '~> 1.2'
24
+ gem.add_development_dependency 'mocha', '~> 0.13'
25
+ gem.add_development_dependency 'simplecov', '~> 0.7'
26
+ gem.add_development_dependency 'guard-minitest', '~> 0.5'
27
+ end
@@ -0,0 +1,68 @@
1
+ require 'spec_helper'
2
+
3
+ module Muffler
4
+ describe Config do
5
+ describe '#initialize' do
6
+ it 'initializes defaults' do
7
+ config = Config.new
8
+ config.header.must_equal 'HTTP_X_MUFFLE_LOGGER'
9
+ config.ips.must_equal []
10
+ config.paths.must_equal []
11
+ end
12
+
13
+ it 'adds default mufflers' do
14
+ muffle_opts = {
15
+ 'HTTP_X_MUFFLE_LOGGER' => '1',
16
+ 'REMOTE_ADDR' => '127.0.0.1',
17
+ 'PATH_INFO' => '/login'
18
+
19
+ }
20
+ dont_muffle_opts = {
21
+ 'REMOTE_ADDR' => '10.1.1.3',
22
+ 'PATH_INFO' => '/assets'
23
+ }
24
+
25
+ config = Config.new
26
+ config.ips << '127.0.0.1'
27
+ config.paths << '/login'
28
+
29
+ config.mufflers[:header].call(muffle_opts).must_equal true
30
+ config.mufflers[:ip].call(muffle_opts).must_equal true
31
+ config.mufflers[:path].call(muffle_opts).must_equal true
32
+
33
+ config.mufflers[:header].call(dont_muffle_opts).must_equal false
34
+ config.mufflers[:ip].call(dont_muffle_opts).must_equal false
35
+ config.mufflers[:path].call(dont_muffle_opts).must_equal false
36
+ end
37
+ end
38
+
39
+ describe '#muffle_with' do
40
+ it 'adds new muffler to mufflers hash' do
41
+ config = Config.new
42
+ config.mufflers.wont_include :test
43
+
44
+ config.muffle_with :test do |opts|
45
+ 'this is a test'
46
+ end
47
+
48
+ config.mufflers.must_include :test
49
+ config.mufflers[:test].call({}).must_equal 'this is a test'
50
+ end
51
+
52
+ it 'replaces an existing muffler in the mufflers hash' do
53
+ config = Config.new
54
+ config.muffle_with :test do |opts|
55
+ 'this is a test'
56
+ end
57
+
58
+ config.mufflers[:test].call({}).must_equal 'this is a test'
59
+
60
+ config.muffle_with :test do |opts|
61
+ 'this is another test'
62
+ end
63
+
64
+ config.mufflers[:test].call({}).must_equal 'this is another test'
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,66 @@
1
+ require 'spec_helper'
2
+
3
+ describe Muffler do
4
+ describe '.muffle?' do
5
+ before do
6
+ Muffler.configure do |config|
7
+ config.ips << '127.0.0.1'
8
+ config.paths << '/assets'
9
+ end
10
+ end
11
+
12
+ it 'returns true if any mufflers return true' do
13
+ Muffler.muffle?('REMOTE_ADDR' => '127.0.0.1', 'PATH_INFO' => '/login').must_equal true
14
+ end
15
+
16
+ it 'returns false if all mufflers return false' do
17
+ Muffler.muffle?('REMOTE_ADDR' => '10.1.1.3', 'PATH_INFO' => '/login').must_equal false
18
+ end
19
+ end
20
+
21
+ describe '.muffle' do
22
+ before do
23
+ @logger = Logger.new(STDOUT)
24
+ @logger.level = Logger::INFO
25
+ end
26
+
27
+ describe 'with a provided log level' do
28
+ it 'changes log level to provided level in block' do
29
+ Muffler.muffle(@logger, Logger::WARN) do
30
+ @logger.level.must_equal Logger::WARN
31
+ end
32
+ end
33
+ end
34
+
35
+ describe 'with no provided log level' do
36
+ it 'changes log level to Logger::ERROR in block' do
37
+ Muffler.muffle(@logger) do
38
+ @logger.level.must_equal Logger::ERROR
39
+ end
40
+ end
41
+ end
42
+
43
+ it 'reverts log level back to original level after block' do
44
+ Muffler.muffle(@logger) {}
45
+ @logger.level.must_equal Logger::INFO
46
+ end
47
+ end
48
+
49
+ describe '.configure' do
50
+ it 'yields the config to a block' do
51
+ Muffler.configure do |config|
52
+ config.must_equal Muffler.config
53
+ end
54
+ end
55
+ end
56
+
57
+ describe '.config' do
58
+ it 'returns the Muffle config object' do
59
+ Muffler.config.must_be_instance_of Muffler::Config
60
+ end
61
+
62
+ it 'memoizes the config object' do
63
+ Muffler.config.object_id.must_equal Muffler.config.object_id
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,12 @@
1
+ if ENV['COVERAGE']
2
+ require 'simplecov'
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ end
6
+ end
7
+
8
+ require 'minitest/spec'
9
+ require 'minitest/autorun'
10
+ require 'mocha'
11
+
12
+ require 'muffler'
metadata ADDED
@@ -0,0 +1,151 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: muffler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Jeremy Israelsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-11-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: bundler
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ~>
36
+ - !ruby/object:Gem::Version
37
+ version: '1.2'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ~>
44
+ - !ruby/object:Gem::Version
45
+ version: '1.2'
46
+ - !ruby/object:Gem::Dependency
47
+ name: mocha
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ~>
52
+ - !ruby/object:Gem::Version
53
+ version: '0.13'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '0.13'
62
+ - !ruby/object:Gem::Dependency
63
+ name: simplecov
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '0.7'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '0.7'
78
+ - !ruby/object:Gem::Dependency
79
+ name: guard-minitest
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ~>
84
+ - !ruby/object:Gem::Version
85
+ version: '0.5'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ~>
92
+ - !ruby/object:Gem::Version
93
+ version: '0.5'
94
+ description: Flexible log suppression for Ruby, Rack, and Rails.
95
+ email:
96
+ - jisraelsen@gmail.com
97
+ executables: []
98
+ extensions: []
99
+ extra_rdoc_files: []
100
+ files:
101
+ - .gitignore
102
+ - .rvmrc
103
+ - .travis.yml
104
+ - Gemfile
105
+ - Guardfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/muffler.rb
110
+ - lib/muffler/config.rb
111
+ - lib/muffler/rack.rb
112
+ - lib/muffler/rack/commonlogger.rb
113
+ - lib/muffler/rack/logger.rb
114
+ - lib/muffler/rails.rb
115
+ - lib/muffler/rails/logger.rb
116
+ - lib/muffler/version.rb
117
+ - muffler.gemspec
118
+ - spec/muffler/config_spec.rb
119
+ - spec/muffler_spec.rb
120
+ - spec/spec_helper.rb
121
+ homepage: http://github.com/jisraelsen/muffler
122
+ licenses: []
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ none: false
129
+ requirements:
130
+ - - ! '>='
131
+ - !ruby/object:Gem::Version
132
+ version: 1.9.2
133
+ required_rubygems_version: !ruby/object:Gem::Requirement
134
+ none: false
135
+ requirements:
136
+ - - ! '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ segments:
140
+ - 0
141
+ hash: 4000724842151476147
142
+ requirements: []
143
+ rubyforge_project:
144
+ rubygems_version: 1.8.24
145
+ signing_key:
146
+ specification_version: 3
147
+ summary: Flexible log suppression library.
148
+ test_files:
149
+ - spec/muffler/config_spec.rb
150
+ - spec/muffler_spec.rb
151
+ - spec/spec_helper.rb