1

有人可以解释一下这种和平的代码是如何工作的,尤其是日志过滤器部分。我想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

4

1 回答 1

0

找到了办法。

  use MongodbLogger::RackMiddleware
  MongodbLogger::RackMiddleware.log_attributes_filter = lambda do |request|
    begin
      request[:params]["Password"] = "[FILTERED]"
      request
    rescue
      request
    end
  end
于 2014-04-08T06:31:42.860 回答