Ruby on Rails provides default logging for "Request", "Response" objects in the controller as well as the time taken etc. Is there a way in which we can accomplish the same in case of Spring Boot without having to write the log statements to print request, response and time taken etc.
PS : Python's Flask has something like Before and After annotations, but I m not sure how we can accomplish Rich Rails like logging in Spring Boot.
How about something like this?
@Configuration
public class ApplicationConfig extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry){
registry.addInterceptor(new ControllerInterceptor()).addPathPatterns(ControllerInterceptor.PATTERN);
}
public class ControllerInterceptor extends HandlerInterceptorAdapter {
public static final String PATTERN = "/mycontrollermappingvalue*";
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
throws Exception {
System.out.println("Before request");
//log values from HttpServletRequest
return true;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
System.out.println("After request");
//log values from HttpServletResponse
}
}
I think you are looking for Java Aspect Oriented Programming. Take a look at this example.
Here is an example of the config entry that ends up logging method calls.
<aop:config>
<aop:aspect id="aspectService" ref="logAspect" >
<aop:pointcut id="pointCutBeforeBC"
expression="execution(* com.test.application.service.*.*(..))" />
<aop:before method="logBefore" pointcut-ref="pointCutBefore" />
</aop:aspect>
<aop:aspect id="aspectUserInterface" ref="logAspect" >
<aop:pointcut id="pointCutBeforeUserInterfaceBA"
expression="execution(* com.test.application.ui.*(..))" />
<aop:before method="pointCutBeforeTraceInput" pointcut-ref="pointCutBeforeUserInterface" />
<aop:after-throwing method="pointCutAfterThrowingOutput" throwing="_Throwable" pointcut-ref="pointCutBeforeUserInterface" />
</aop:aspect>
</aop:config>
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.