简体   繁体   中英

How to log web request and response in Spring?

I want to know what parts of my web page the users use and then present it in a nice graphic interface. The only thing I could come up with was this:

@Controller
public class WebController {

    @Autowired
    private DatabaseResource dbr;

    @RequestMapping("/foobar")
    public String getFoobar(Principal principal, ModelMap model, HttpServletRequest req) {
    dbr.logVisit(req); //write the request to a database
 //...

At each @RequestMapping I write the request to the database. Is this the best way to do it?

Does it exists a library for this so I do not have to write it all my self? A package that works a bit like Eclipse Usage Data Collector.

I'm using Spring and Spring Security.

Write a custom HandlerInterceptor , this feature is specificaly designed to implement features like this.

Another approach is to use Logback Access that tightly and seamlessly integrates with Tomcat.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM