Sha256: 9ec34b9537d4248df354a21abfd7f810709e76a241fb0af63aeeaa3239bdc0ad

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

module Silencer
  RailsLogger = if Silencer::Environment.rails2?
    require 'rails/rack/log_tailer'
    ::Rails::Rack::LogTailer
  else
    require 'rails/rack/logger'
    ::Rails::Rack::Logger
  end

  module Rails
    class Logger < RailsLogger
      include Silencer::Util
      include Silencer::Hush

      def initialize(app, *args)
        opts     = extract_options!(args)
        @silence = wrap(opts.delete(:silence))
        @routes  = {
          'OPTIONS' => wrap(opts.delete(:options)) + @silence,
          'GET'     => wrap(opts.delete(:get)) + @silence,
          'HEAD'    => wrap(opts.delete(:head)) + @silence,
          'POST'    => wrap(opts.delete(:post)) + @silence,
          'PUT'     => wrap(opts.delete(:put)) + @silence,
          'DELETE'  => wrap(opts.delete(:delete)) + @silence,
          'TRACE'   => wrap(opts.delete(:trace)) + @silence,
          'CONNECT' => wrap(opts.delete(:connect)) + @silence,
          'PATCH'   => wrap(opts.delete(:patch)) + @silence,
        }

        if normalized_args = normalize(args)
          super(app, normalized_args)
        else
          super(app)
        end
      end

      def call(env)
        old_logger_level     = ::Rails.logger.level
        ::Rails.logger.level = ::Logger::ERROR if silence_request?(env)

        super
      ensure
        # Return back to previous logging level
        ::Rails.logger.level = old_logger_level
      end

      private

      def normalize(args)
        args = case args.size
        when 0 then nil
        when 1 then args.shift
        else args
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
silencer-1.0.0.rc3 lib/silencer/rails/logger.rb