Skip to content

Commit 75ac7de

Browse files
author
mokhan
committed
Updated custom logging implementation
1 parent f053451 commit 75ac7de

4 files changed

Lines changed: 32 additions & 6 deletions

File tree

‎spring-rest-logging/pom.xml‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<artifactId>spring-boot-starter-parent</artifactId>
1313
<version>1.4.3.RELEASE</version>
1414
</parent>
15-
15+
1616
<dependencies>
1717
<!-- Spring Boot Dependencies -->
1818
<dependency>
@@ -60,5 +60,14 @@
6060
<groupId>org.springframework</groupId>
6161
<artifactId>spring-test</artifactId>
6262
</dependency>
63+
64+
<!-- Apache common IO for utility -->
65+
<dependency>
66+
<groupId>commons-io</groupId>
67+
<artifactId>commons-io</artifactId>
68+
<version>2.4</version>
69+
</dependency>
70+
71+
6372
</dependencies>
6473
</project>

‎spring-rest-logging/src/main/java/com/baeldung/rest/log/app/TaxiFareRequestInterceptor.java‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import org.slf4j.LoggerFactory;
88
import org.springframework.stereotype.Component;
99
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
10+
import org.springframework.web.util.ContentCachingRequestWrapper;
11+
12+
import com.baeldung.rest.log.util.RequestLoggingUtil;
1013

1114
@Component
1215
public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
@@ -15,7 +18,21 @@ public class TaxiFareRequestInterceptor extends HandlerInterceptorAdapter {
1518

1619
@Override
1720
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
18-
LOGGER.info("REQUEST URI: " + request.getRequestURI());
21+
String postData = null;
22+
HttpServletRequest requestCacheWrapperObject = null;
23+
try {
24+
// Uncomment to produce the stream closed issue
25+
// postData = RequestLoggingUtil.getStringFromInputStream(request.getInputStream());
26+
27+
// To overcome request stream closed issue
28+
requestCacheWrapperObject = new ContentCachingRequestWrapper(request);
29+
requestCacheWrapperObject.getParameterMap();
30+
} catch (Exception exception) {
31+
exception.printStackTrace();
32+
} finally {
33+
postData = RequestLoggingUtil.readPayload(requestCacheWrapperObject);
34+
LOGGER.info("REQUEST DATA: " + postData);
35+
}
1936
return true;
2037
}
2138

‎spring-rest-logging/src/main/java/com/baeldung/rest/log/config/RequestLoggingFilterConfig.java‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package com.baeldung.rest.log.config;
22

3-
import javax.servlet.annotation.WebFilter;
4-
53
import org.springframework.context.annotation.Bean;
64
import org.springframework.context.annotation.Configuration;
75
import org.springframework.web.filter.CommonsRequestLoggingFilter;
86

97
@Configuration
10-
@WebFilter
118
public class RequestLoggingFilterConfig {
129

1310
@Bean

‎spring-rest-logging/src/main/java/com/baeldung/rest/log/controller/TaxiFareController.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ public RateCard getTaxiFare() {
3434
@ResponseBody
3535
public String calculateTaxiFare(@RequestBody @Valid TaxiRide taxiRide) {
3636
LOGGER.debug("calculateTaxiFare() - START");
37-
return taxiFareCalculatorService.calculateFare(taxiRide);
37+
String totalFare = taxiFareCalculatorService.calculateFare(taxiRide);
38+
LOGGER.debug("calculateTaxiFare() - Total Fare : {}",totalFare);
39+
LOGGER.debug("calculateTaxiFare() - END");
40+
return totalFare;
3841
}
3942

4043
}

0 commit comments

Comments
 (0)