Sha256: bdc2715bc522361fe72cb2e99706bb6c09d27ef69a2a6a3f865f27bc1ca2f6ab

Contents?: true

Size: 1.49 KB

Versions: 1

Compression:

Stored size: 1.49 KB

Contents

# log file when processing
module RailsMiddlewareLog
  class Log
    class << self
      def init(middlewares)
        middlewares.each do |middleware|
          klass = middleware_class(middleware)
          if klass && klass.method_defined?(:call)
            klass.class_eval do
              alias_method :old_call, :call

              def call(env)
                RailsMiddlewareLog::Log.before_call(self.class, env)
                old_call(env)
              end
            end
          end
        end
      end

      # get the current middleware class
      def middleware_class(middleware)
        case middleware.klass
        when ActiveSupport::Cache::Strategy::LocalCache::Middleware
          middleware.klass.class
        when Class
          middleware.klass
        else
          nil
        end
      end

      def before_call(k_class, env)
        if config_url_match?(env['PATH_INFO'])
          if block = RailsMiddlewareLog.config.before_middleware
            block.call(k_class, env)
          else
            Rails.logger.info "-------#{k_class.name}"
          end
        end
      end

      # check current request
      def config_url_match?(url)
        target_urls = RailsMiddlewareLog.config.target_urls
        case target_urls
        when true
          true
        when String
          target_urls == url
        when Array
          target_urls.include? url
        when Regexp
          target_urls === url
        else
          false
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_middleware_log-0.1.0 lib/rails_middleware_log/log.rb