轻松抑制 Rails 日志器
Silencer is a simple rack-middleware for Rails that can selectively disable logging on per-action basis. It's based on a blog post by Dennis Reimann.
Note: Silencer is only threadsafe in Rails version 4.2.6 and later.
Just add silencer to your Gemfile:
gem 'silencer', require: falseNote: 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.
In version 2.0, you'll need to require the correct logger within your application. See the Usage documentation for more details.
Create an initializer (like config/initializers/silencer.rb) with the contents:
require 'silencer/rails/logger'
Rails.application.configure do
config.middleware.swap(
Rails::Rack::Logger,
Silencer::Logger,
config.log_tags,
silence: ["/noisy/action.json"]
)
endrequire 'silencer/rack/logger'
use Silencer::Logger, silence: ["/noisy/action.json"]Or if you'd prefer, you can pass it regular expressions:
config.middleware.swap(
Rails::Rack::Logger,
Silencer::Logger,
config.log_tags,
silence: [%r{^/assets/}]
)Or you can silence specific request methods only:
config.middleware.swap(
Rails::Rack::Logger,
Silencer::Logger,
config.log_tags,
get: [%r{^/assets/}],
post: [%r{^/some_path}]
)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.
Silencer supports the following configuration options.
| Configuration | Description | Default |
|---|---|---|
silence |
Silences matching requests regardless of request method | None |
get |
Silences matching GET requests | None |
head |
Silences matching HEAD requests | None |
post |
Silences matching POST requests | None |
put |
Silences matching PUT requests | None |
delete |
Silences matching DELETE requests | None |
patch |
Silences matching PATCH requests | None |
trace |
Silences matching TRACE requests | None |
connect |
Silences matching CONNECT requests | None |
options |
Silences matching OPTIONS requests | None |
enable_header |
Enable/disable X-SILENCE-LOGGER header support | true |
Copyright (c) 2012 Steve Agalloco. See LICENSE for details.
暂无开放 Issues,或尚未同步最近议题。