Sha256: 8adfc3881ff5b3ea0e88beca3d24cf32fde9825a657633d43e207f7b3d8230a8

Contents?: true

Size: 893 Bytes

Versions: 1

Compression:

Stored size: 893 Bytes

Contents

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)
      old_logger_level = Rails.logger.level
      Rails.logger.level = ::Logger::ERROR if silence_request?(env)

      @app.call(env)
    ensure
      # Return back to previous logging level
      Rails.logger.level = old_logger_level
    end

    private

    def silence_request?(env)
      silent_header?(env) || silent_request_path?(env) || silent_request_match?(env)
    end

    def silent_header?(env)
      env['X-SILENCE-LOGGER']
    end

    def silent_request_path?(env)
      @opts[:silence].include?(env['PATH_INFO'])
    end

    def silent_request_match?(env)
      @opts[:silence].select { |s| s.kind_of?(Regexp) }.any? { |s| s =~ env['PATH_INFO'] }
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
silencer-0.2.0 lib/silencer/logger.rb