给出下面的代码如何在线程结束时只记录一个最终请求时间时如何记录请求时间?
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
new ClientRequestContextHolder();
logger.info("request filter ("+ ClientRequestContextHolder.getContext().getAttribute("uniqueId") +") started");
filterChain.doFilter(request, response);
logger.info("request filter ("+ ClientRequestContextHolder.getContext().getAttribute("uniqueId") +") finished");
long st = (long)ClientRequestContextHolder.getContext().getAttribute("request_start");
logger.info("RequestTime: " + customFormat("###,###.###",(System.nanoTime() - st)) + "ns");
}
我当前的输出看起来像这样
INFO : com.app.context.ClientRequestContextHolder - initialized
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 7,958,694ns
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 12,657,404ns
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 13,942,554ns
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 14,579,297ns
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 16,428,276ns
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 17,329,786ns
INFO : com.app.filters.ClientRequestFilter - request filter (34) started
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 42,787,490ns
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 43,226,233ns
INFO : com.app.filters.ClientRequestFilter - request filter (34) finished
INFO : com.app.filters.ClientRequestFilter - RequestTime: 45,455,568ns
答案 0 :(得分:0)
得到了它。
由于过滤器chaing是递归的,我只是添加了一个深层控件。在最后,如果深度为0,那么我计算打印结果。