4

我有一个带有 /health/api 端点的 Sintra 应用程序,如下所述,它被我的负载均衡器每秒调用多次。我想仅删除此路由的默认日志记录,或者相反仅将我关心的端点打印到日志中。

get '/health/api' do
  # Health Check
  'I keep quiet'
end

get '/members' do
  # get members data
  'This request gets logged'
end
4

1 回答 1

7

您不能将默认的 Rack::CommonLogger 配置为只打印一些请求,而对其他请求保持安静。但是您可以禁用默认的 CommonLogger 并使用您自己的过滤功能:

require 'rubygems'
require 'rack'
require 'sinatra'

LOGGING_BLACKLIST = ['/health/api']

class FilteredCommonLogger < Rack::CommonLogger
  def call(env)
    if filter_log(env)
      # default CommonLogger behaviour: log and move on
      super
    else
      # pass request to next component without logging
      @app.call(env)
    end
  end

  # return true if request should be logged
  def filter_log(env)
    !LOGGING_BLACKLIST.include?(env['PATH_INFO'])
  end
end

disable :logging
use FilteredCommonLogger

get '/members' do
  # get members data
  'This request gets logged'
end

get '/health/api' do
  # Health Check
  'I keep quiet'
end

您显然也可以编写只记录“白名单”请求的自定义记录器。

于 2012-11-21T21:54:42.347 回答