Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to log properly http requests with Spring MVC

Hello I've been trying to figure out generic way to log http requests in my application, so far no luck, here is how I handle the logging right now i.e:

@RequestMapping(value="register", method = RequestMethod.POST)     @ResponseBody     public String register(@RequestParam(value="param1",required=false) String param1, @RequestParam("param2") String param2, @RequestParam("param3") String param3, HttpServletRequest request){         long start = System.currentTimeMillis();         logger.info("!--REQUEST START--!");          logger.info("Request URL: " + request.getRequestURL().toString());          List<String> requestParameterNames = Collections.list((Enumeration<String>)request.getParameterNames());         logger.info("Parameter number: " + requestParameterNames.size());    for (String parameterName : requestParameterNames){            logger.info("Parameter name: " + parameterName + " - Parameter value: " + request.getParameter(parameterName));         }                   //Some processing logic, call to the various services/methods with different parameters, response is always String(Json)         String response = service.callSomeServiceMethods(param1,param2,param3);  logger.info("Response is: " + response);          long end = System.currentTimeMillis();         logger.info("Requested completed in: " + (end-start) + "ms");         logger.info("!--REQUEST END--!");             return response;     } 

So what I do right now for different controllers/methods is copy everything from beginning of the inside of the method until the processing logic which differs from method to method and then copy everything from below of that as showed in above template.

It is kind of messy, and there is a lot of code repetition(which I don't like). But I need to log everything.

Does anyone have more experience with this kinds of logging, can anyone shed some light on this?

like image 804
London Avatar asked Jul 08 '11 22:07

London


People also ask

How request is handled in Spring MVC?

DispatcherServlet as the Heart of Spring MVCmapping an HTTP request to a certain processing method. parsing of HTTP request data and headers into data transfer objects (DTOs) or domain objects. model-view-controller interaction. generation of responses from DTOs, domain objects, etc.

How does Spring MVC handle multiple requests?

In Spring, every request is executed in a separate thread. For example, when two users want to log in at the same time, the JVM creates two threads: one thread for the first user and another one for the second user. And these threads work with the singleton bean separately.


1 Answers

EDIT: Also, see @membersound's comment on this answer, which improves this answer.

Spring supports this. See CommonsRequestLoggingFilter. If using Spring Boot, just register a bean of that type and Boot will apply it to the filter chain. Like:

@Bean public Filter logFilter() {     CommonsRequestLoggingFilter filter = new CommonsRequestLoggingFilter();     filter.setIncludeQueryString(true);     filter.setIncludePayload(true);     filter.setMaxPayloadLength(5120);     return filter; } 

Also, this logging filter requires the log level be set to DEBUG. E.g. do this in a logback.xml with:

<logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="DEBUG"/> 
like image 132
David Groomes Avatar answered Sep 22 '22 23:09

David Groomes