Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
stve committed Oct 13, 2011
0 parents commit ccacdee
Show file tree
Hide file tree
Showing 17 changed files with 228 additions and 0 deletions.
Empty file added .gemtest
Empty file.
17 changes: 17 additions & 0 deletions .gitignore
@@ -0,0 +1,17 @@
*.gem
*.rbc
.bundle
.config
.yardoc
Gemfile.lock
InstalledFiles
_yardoc
coverage
doc/
lib/bundler/man
pkg
rdoc
spec/reports
test/tmp
test/version_tmp
tmp/*.log
3 changes: 3 additions & 0 deletions .rspec
@@ -0,0 +1,3 @@
--color
--format=nested
--backtrace
1 change: 1 addition & 0 deletions .simplecov
@@ -0,0 +1 @@
SimpleCov.start
1 change: 1 addition & 0 deletions .yardopts
@@ -0,0 +1 @@
--markup markdown
4 changes: 4 additions & 0 deletions Gemfile
@@ -0,0 +1,4 @@
source 'http://rubygems.org'

# Specify your gem's dependencies in quietus.gemspec
gemspec
21 changes: 21 additions & 0 deletions Guardfile
@@ -0,0 +1,21 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }

# Rails example
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
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"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('spec/spec_helper.rb') { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end

20 changes: 20 additions & 0 deletions LICENSE.md
@@ -0,0 +1,20 @@
Copyright (c) 2011 Steve Agalloco

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44 changes: 44 additions & 0 deletions README.md
@@ -0,0 +1,44 @@
Silencer
========

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.

Installation
------------

Just add silencer to your Gemfile:

gem 'silencer'

Usage
-----

In your production environment (presumably):

```ruby
require 'silencer/logger'

module MyApp
class Application < Rails::Application
config.middleware.swap Rails::Rack::Logger, Silencer::Logger, :silence => ["/noisy/action.json"]
end
end
```

By default, Silencer's logger will serve as a drop-in replacement for Rails default logger, but you can also send it a 'X-SILENCE-LOGGER' header (with any value) and that will also produce the same behavior.

Note on Patches/Pull Requests
-----------------------------

* Fork the project.
* Make your feature addition or bug fix.
* Add tests for it. This is important so I don't break it in a
future version unintentionally.
* Commit, do not mess with rakefile, version, or history.
(if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
* Send me a pull request. Bonus points for topic branches.

Copyright
---------

Copyright (c) 2011 Steve Agalloco. See LICENSE for details.
21 changes: 21 additions & 0 deletions Rakefile
@@ -0,0 +1,21 @@
#!/usr/bin/env rake
require "bundler/gem_tasks"

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task :test => :spec

namespace :doc do
require 'yard'
YARD::Rake::YardocTask.new do |task|
task.files = ['lib/**/*.rb']
task.options = [
'--protected',
'--output-dir', 'doc/yard',
'--markup', 'markdown',
'--readme', 'README.md'
]
end
end
1 change: 1 addition & 0 deletions lib/silencer.rb
@@ -0,0 +1 @@
require 'silencer/logger'
21 changes: 21 additions & 0 deletions lib/silencer/logger.rb
@@ -0,0 +1,21 @@
require 'rails/rack/logger'

module Silencer
class Logger < Rails::Rack::Logger
def initialize(app, opts = {})
@app = app
@opts = opts
@opts[:silence] ||= []
end

def call(env)
if env['X-SILENCE-LOGGER'] || @opts[:silence].include?(env['PATH_INFO'])
Rails.logger.silence do
@app.call(env)
end
else
super(env)
end
end
end
end
3 changes: 3 additions & 0 deletions lib/silencer/version.rb
@@ -0,0 +1,3 @@
module Silencer
VERSION = "0.0.1"
end
27 changes: 27 additions & 0 deletions silencer.gemspec
@@ -0,0 +1,27 @@
# -*- encoding: utf-8 -*-
require File.expand_path('../lib/silencer/version', __FILE__)

Gem::Specification.new do |gem|
gem.name = "silencer"
gem.version = Silencer::VERSION
gem.authors = ["Steve Agalloco"]
gem.email = ["steve.agalloco@gmail.com"]
gem.homepage = "http://github.com/spagalloco/silencer"
gem.description = 'Selectively quiet your Rails logger on a per-action basis'
gem.summary = gem.description

gem.add_development_dependency('rake', '~> 0.9')
gem.add_development_dependency('rspec', '~> 2.6')
gem.add_development_dependency('yard', '~> 0.7')
gem.add_development_dependency('rdiscount', '~> 1.6')
gem.add_development_dependency('simplecov', '~> 0.5')
gem.add_development_dependency('guard', '~> 0.8')
gem.add_development_dependency('guard-rspec', '~> 0.5')

gem.add_dependency 'railties', '~> 3'

gem.files = `git ls-files`.split("\n")
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
gem.require_paths = ["lib"]
end
35 changes: 35 additions & 0 deletions spec/silencer_spec.rb
@@ -0,0 +1,35 @@
require 'spec_helper'

describe Silencer::Logger do
before(:each) do
@app = lambda { |env| [200, {}, ''] }

Rails.logger = ActiveSupport::BufferedLogger.new('tmp/test.log')
end

it 'allows log writing when not implemented' do
Rails.logger.should_not_receive(:silence)
Rails.logger.should_receive(:info).at_least(:once)

Silencer::Logger.new(@app).call(Rack::MockRequest.env_for("/"))
end

it 'quiets the log when configured with a silenced path' do
Rails.logger.should_receive(:silence)
Rails.logger.should_not_receive(:info)

Silencer::Logger.new(@app, :silence => ['/']).call(Rack::MockRequest.env_for("/"))
end

it 'quiets the log when passed a custom header "X-SILENCE-LOGGER"' do
Rails.logger.should_receive(:silence)
Rails.logger.should_not_receive(:info)

Silencer::Logger.new(@app).call(Rack::MockRequest.env_for("/", 'X-SILENCE-LOGGER' => 'true'))
end

it 'does not tamper with the response' do
response = Silencer::Logger.new(@app).call(Rack::MockRequest.env_for("/", 'X-SILENCE-LOGGER' => 'true'))
response[0].should eq(200)
end
end
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,9 @@
require 'simplecov'

$LOAD_PATH.unshift(File.dirname(__FILE__))
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))

require 'rack'
require 'rails'
require 'silencer'
require 'rspec'
Empty file added tmp/.gitkeep
Empty file.

0 comments on commit ccacdee

Please sign in to comment.