在我的 springboot 應(yīng)用程序中,我們使用 and 記錄控制器請(qǐng)求/響應(yīng)。有關(guān)示例代碼,請(qǐng)參閱下面的鏈接。org.springframework.web.util.ContentCachingRequestWrapperorg.springframework.web.util.ContentCachingResponseWrapper自定義日志過(guò)濾器 :https://stackoverflow.com/a/42023374/1958669在應(yīng)用程序中有一個(gè)存在?,F(xiàn)在的問(wèn)題是在記錄它的唯一攔截器名稱時(shí) className 。因此,為了獲取實(shí)際控制器名稱,我嘗試在攔截器中獲取類(lèi)名。但這給出了.interceptororg.springframework.web.util.ContentCachingRequestWrapperCustomWebMvcConfig :@Configurationpublic class CustomWebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new CustomLogInterceptor()); }}自定義日志接收器 :public class CustomLogInterceptor extends HandlerInterceptorAdapter { private static final Logger logger = LogManager.getLogger(CustomLogInterceptor.class); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { return true; } @Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception{ //Some operation }}獲取以下格式的日志:time=“2019-03-18T09:59:51,001Z”, thread=“[o-auto-1exec-]” ,class=“a.b.c.CustomLoggingFilter ”,message=“Payload_1”time=“2019-03-18T09:59:51,001Z”, thread=“[o-auto-1-xec-1]” ,class=“a.b.c.CustomLoggingFilter ”,message=“Payload_2””我的問(wèn)題是如何在日志中獲取實(shí)際的 ControllerClass 名稱,而不是 LoggingFilterClass(一個(gè)使用org.springframework.web.util.ContentCachingRequestWrapper)
1 回答

米脂
TA貢獻(xiàn)1836條經(jīng)驗(yàn) 獲得超3個(gè)贊
您可以嘗試使用:
HandlerMethod handlerMethod = (HandlerMethod) handler;
String controllerName = handlerMethod.getBeanType().getSimpleName().replace("Controller", "");
或者,如果要打印 URI:
request.getRequestURI();
我認(rèn)為您無(wú)法更改記錄器,因?yàn)槟恢勒l(shuí)將成為控制器。
添加回答
舉報(bào)
0/150
提交
取消