In my springboot application, we are logging controller requests/responses using the org.springframework.web.util.ContentCachingRequestWrapper
and org.springframework.web.util.ContentCachingResponseWrapper
. Refer below link for the sample code .
CustomLoggingFilter :
There is a interceptor
present in the application. Now the problem is while logging its only the interceptor name coming in className . So, for getting the actual controller name , I tried getting classname in interceptor . But that gives org.springframework.web.util.ContentCachingRequestWrapper
.
CustomWebMvcConfig :
@Configuration
public class CustomWebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new CustomLogInterceptor());
}
}
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
}
}
Getting logs in below format:
time="2019-03-18T09:59:51,001Z", thread="[o-auto-1exec-]" ,class=“abcCustomLoggingFilter ",message=“Payload_1“
time="2019-03-18T09:59:51,001Z", thread="[o-auto-1-xec-1]" ,class=“abcCustomLoggingFilter ",message=“Payload_2“"
My Question is how to get the actual ControllerClass name in logs instead of the LoggingFilterClass (one which uses the org.springframework.web.util.ContentCachingRequestWrapper
)
You can try with:
HandlerMethod handlerMethod = (HandlerMethod) handler;
String controllerName = handlerMethod.getBeanType().getSimpleName().replace("Controller", "");
Or if you want to print the URI:
request.getRequestURI();
I think you can't change the logger as you don't know who would be the controller.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.