Skip to content

Instantly share code, notes, and snippets.

@maltoe
Last active August 29, 2015 14:00
Show Gist options
  • Save maltoe/11147730 to your computer and use it in GitHub Desktop.
Save maltoe/11147730 to your computer and use it in GitHub Desktop.
## app/controllers/application_controller.rb
class ApplicationController < ActionController::Base
@@silenced_actions = [:home]
def home
end
end
## config/initializers/init_quiet_logger.rb
# Set our own QuietLogger as the default logger.
Rails::Application.config.middleware.swap Rails::Rack::Logger, Utils::QuietLogger
## lib/utils/quiet_logger
module Utils
class QuietLogger < Rails::Rack::Logger
def initialize(app, opts = {})
@app = app
@opts = opts
super
end
def call(env)
route = Rails.application.routes.recognize_path(env['PATH_INFO'])
action = route[:action]
controller = route[:controller]
# Had to go wild here since we're using nested controllers.
controller_klass = (controller.split('/').map { |p| p.camelize }.join('::') + 'Controller').constantize
if controller_klass.class_variable_defined?(:@@silenced_actions) &&
controller_klass.class_variable_get(:@@silenced_actions).include?(action.to_sym)
log_only_errors do
@app.call(env)
end
else
super(env)
end
rescue # recognize_path might throw RoutingError if some static dispatcher or else comes after QuietLogger.
super(env)
end
def log_only_errors
old_level = Rails.logger.level
Rails.logger.level = Logger::ERROR
r = yield
Rails.logger.level = old_level
r
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment