1

Can someone please explain how this peace of code works, especially the log filter part. I want to assign log_attributes_filter from the rack file, where i calling use MongodbLogger::RackMiddleware, how to do so?

# 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

Before i simply used it as use MongodbLogger::RackMiddleware, but now i want to filter some params, how should i properly assign @@log_attributes_filter?

Petya petrov
  • 2,153
  • 5
  • 23
  • 35
  • From reading http://mongodb-logger.catware.org/ and https://github.com/le0pard/mongodb_logger it seems that filters are set via the web-interface, I don't think you need to write anything specific here, unless you want to extend it beyond it's current capabilities. If that's the case I might consider reaching out to smotsovoy as he wrote that patch https://github.com/le0pard/mongodb_logger/pull/48 . – Mike H-R Apr 07 '14 at 17:08

1 Answers1

0

Found a way.

  use MongodbLogger::RackMiddleware
  MongodbLogger::RackMiddleware.log_attributes_filter = lambda do |request|
    begin
      request[:params]["Password"] = "[FILTERED]"
      request
    rescue
      request
    end
  end
Petya petrov
  • 2,153
  • 5
  • 23
  • 35