Updated custom logging implementation

This commit is contained in:
mokhan 2017-07-21 22:12:27 +05:30
parent f05345112f
commit 75ac7de5cd
4 changed files with 32 additions and 6 deletions

View File

@ -12,7 +12,7 @@
<artifactId>spring-boot-starter-parent</artifactId> <artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.3.RELEASE</version> <version>1.4.3.RELEASE</version>
</parent> </parent>
<dependencies> <dependencies>
<!-- Spring Boot Dependencies --> <!-- Spring Boot Dependencies -->
<dependency> <dependency>
@ -60,5 +60,14 @@
<groupId>org.springframework</groupId> <groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId> <artifactId>spring-test</artifactId>
</dependency> </dependency>
<!-- Apache common IO for utility -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
</dependencies> </dependencies>
</project> </project>

View File

@ -7,6 +7,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.ContentCachingRequestWrapper;
import com.baeldung.rest.log.util.RequestLoggingUtil;
@Component @Component
public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter { public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
@ -15,7 +18,21 @@ public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
@Override @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
LOGGER.info("REQUEST URI: " + request.getRequestURI()); String postData = null;
HttpServletRequest requestCacheWrapperObject = null;
try {
// Uncomment to produce the stream closed issue
// postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream());
// To overcome request stream closed issue
requestCacheWrapperObject = new ContentCachingRequestWrapper(request);
requestCacheWrapperObject.getParameterMap();
} catch (Exception exception) {
exception.printStackTrace();
} finally {
postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject);
LOGGER.info("REQUEST DATA: " + postData);
}
return true; return true;
} }

View File

@ -1,13 +1,10 @@
package com.baeldung.rest.log.config; package com.baeldung.rest.log.config;
import javax.servlet.annotation.WebFilter;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CommonsRequestLoggingFilter; import org.springframework.web.filter.CommonsRequestLoggingFilter;
@Configuration @Configuration
@WebFilter
public class RequestLoggingFilterConfig { public class RequestLoggingFilterConfig {
@Bean @Bean

View File

@ -34,7 +34,10 @@ public class TaxiFareController {
@ResponseBody @ResponseBody
public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) { public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) {
LOGGER.debug("calculateTaxiFare() - START"); LOGGER.debug("calculateTaxiFare() - START");
return taxiFareCalculatorService.calculateFare(taxiRide); String totalFare = taxiFareCalculatorService.calculateFare(taxiRide);
LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare);
LOGGER.debug("calculateTaxiFare() - END");
return totalFare;
} }
} }