我有一个Sintra应用程序,其中包含一个/health/api端点(如下所述),我的负载均衡器每秒钟会多次调用该端点。我想要删除仅针对此路由的默认日志记录,或者相反地,仅打印我关心的端点到日志中。
get '/health/api' do
# Health Check
'I keep quiet'
end
get '/members' do
# get members data
'This request gets logged'
end
您不能将默认的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
您显然也可以编写自定义日志记录器,仅记录“白名单”请求。
p t = env["PATH_INFO"] + ?? + env["QUERY_STRING"]; if t[/^\/log\?/] ; @app.call env ; else ; super ; end
但不知何故我仍然看到:naki-PC - - [17/Jul/2014:19:23:17 ╠юёъютёъюх тЁхь (чшьр)] "GET /log?position=2240 HTTP/1.1" 200 2 http://localhost:8719/ -> /log?position=2240 "/log?position=2240"
- Nakilon