0

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 :

https://stackoverflow.com/a/42023374/1958669

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=“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“"

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)

Community
  • 1
  • 1
shashantrika
  • 1,039
  • 1
  • 9
  • 29

1 Answers1

1

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.

NikNik
  • 2,191
  • 2
  • 15
  • 34