机架中间件全球中间件

时间:2014-04-07 13:25:45

标签: ruby-on-rails ruby rack middleware rack-middleware

有人可以解释一下代码的安静性,特别是日志过滤器部分。 我想从机架文件中分配log_attributes_filter,我在哪里拨打use MongodbLogger::RackMiddleware,该怎么做?

# Rack middleware for mounted rack app (e.g Grape)
module MongodbLogger
  class RackMiddleware
    @@log_attributes_filter = nil

    def self.log_attributes_filter=(filter)
      @@log_attributes_filter = filter
    end

    def initialize(app)
      @app = app
    end

    def call(env)
      request = ::Rack::Request.new env
      path = request.path.split('/')
      log_attrs = {
        params:     request.params
      }

      log_attrs = @@log_attributes_filter.call(log_attrs) if @@log_attributes_filter

      logger.mongoize(log_attrs) do
        return @app.call(env)
      end
    end
  end
end

之前我只是将其用作use MongodbLogger::RackMiddleware,但现在我想过滤一些参数,我该如何正确分配@@log_attributes_filter

1 个答案:

答案 0 :(得分:0)

找到了一条路。

  use MongodbLogger::RackMiddleware
  MongodbLogger::RackMiddleware.log_attributes_filter = lambda do |request|
    begin
      request[:params]["Password"] = "[FILTERED]"
      request
    rescue
      request
    end
  end