Skip to content

Commit 69addd9

Browse files
committed
docs: add logs
1 parent d9b7cf9 commit 69addd9

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-5
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package fr.insee.genesis.configuration;
2+
3+
import jakarta.servlet.FilterChain;
4+
import jakarta.servlet.ServletException;
5+
import jakarta.servlet.annotation.WebFilter;
6+
import jakarta.servlet.http.HttpServletRequest;
7+
import jakarta.servlet.http.HttpServletResponse;
8+
import lombok.extern.slf4j.Slf4j;
9+
import org.springframework.core.annotation.Order;
10+
import org.springframework.stereotype.Component;
11+
import org.springframework.web.filter.OncePerRequestFilter;
12+
import org.springframework.web.util.ContentCachingRequestWrapper;
13+
import org.springframework.web.util.ContentCachingResponseWrapper;
14+
15+
import java.io.IOException;
16+
import java.nio.charset.StandardCharsets;
17+
18+
19+
@Component
20+
@WebFilter(urlPatterns = "/*")
21+
@Order(-999)
22+
@Slf4j
23+
public class LogRequestFilter extends OncePerRequestFilter {
24+
25+
private static final String REQUEST_MESSAGE_FORMAT =
26+
"CALL {} {} - "
27+
// + "Content-Type : {} \n "
28+
// + "Headers : {} \n "
29+
+ "Params : {} - "
30+
+ "Body : {} \n ";
31+
32+
private static final String RESPONSE_MESSAGE_FORMAT =
33+
"END {} {} - "
34+
+ "Status : {} - "
35+
// + "Content-Type : {} \n "
36+
// + "Headers : {} \n "
37+
+ "Body : {} \n";
38+
39+
@Override
40+
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
41+
throws ServletException, IOException {
42+
43+
//Cache request to avoid calling twice the same inputStream
44+
ContentCachingRequestWrapper req = new ContentCachingRequestWrapper(request);
45+
ContentCachingResponseWrapper resp = new ContentCachingResponseWrapper(response);
46+
47+
log.info(REQUEST_MESSAGE_FORMAT,
48+
req.getMethod(), req.getRequestURI(),
49+
// req.getContentType(),
50+
// new ServletServerHttpRequest(req).getHeaders(), //Headers
51+
request.getQueryString(),//Params
52+
new String(req.getContentAsByteArray(), StandardCharsets.UTF_8));//Body
53+
54+
55+
// Execution request chain
56+
filterChain.doFilter(req, resp);
57+
58+
59+
log.info(RESPONSE_MESSAGE_FORMAT,
60+
req.getMethod(), req.getRequestURI(),
61+
resp.getStatus(),
62+
getResponseBody(req, resp)); //Body
63+
64+
// Finally remember to respond to the client with the cached data.
65+
resp.copyBodyToResponse();
66+
}
67+
68+
private String getResponseBody(ContentCachingRequestWrapper req, ContentCachingResponseWrapper resp) {
69+
if (req.getRequestURI().contains("swagger-ui") ||req.getRequestURI().contains("api-docs")) return "Hidden Swagger response";
70+
return new String(resp.getContentAsByteArray(), StandardCharsets.UTF_8);
71+
}
72+
73+
}

src/main/java/fr/insee/genesis/controller/rest/ScheduleController.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,13 @@ public ResponseEntity<Object> addSchedule(
8181
encryptionVaultPath,
8282
useSignature
8383
);
84-
log.info("New schedule request for survey {} with encryption", surveyName);
8584
scheduleApiPort.addSchedule(surveyName,
8685
serviceToCall == null ? ServiceToCall.MAIN : serviceToCall,
8786
frequency,
8887
scheduleBeginDate,
8988
scheduleEndDate,
9089
trustParameters);
9190
}else{
92-
log.info("New schedule request for survey {}", surveyName);
9391
scheduleApiPort.addSchedule(surveyName,
9492
serviceToCall == null ? ServiceToCall.MAIN : serviceToCall,
9593
frequency,
@@ -112,7 +110,6 @@ public ResponseEntity<Object> deleteSchedule(
112110
@Parameter(description = "Survey name of the schedule(s) to delete") @RequestParam("surveyName") String surveyName
113111
){
114112
try {
115-
log.info("Delete schedule request for survey {}", surveyName);
116113
scheduleApiPort.deleteSchedule(surveyName);
117114
}catch (NotFoundException e){
118115
log.warn("Survey {} not found for deletion !", surveyName);
@@ -130,7 +127,6 @@ public ResponseEntity<Object> setSurveyLastExecution(
130127
@Parameter(description = "Date to save as last execution date", example = "2024-01-01T12:00:00") @RequestParam("newDate") LocalDateTime newDate
131128
) {
132129
try {
133-
log.debug("Got update last execution on {} with data param {}", surveyName, newDate);
134130
scheduleApiPort.updateLastExecutionName(surveyName, newDate);
135131
log.info("{} last execution updated at {} !", surveyName, newDate);
136132
}catch (NotFoundException e){

src/main/java/fr/insee/genesis/controller/rest/UtilsController.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ public ResponseEntity<Object> saveResponsesFromXmlFile(@RequestParam("inputFolde
4040
@RequestParam("filename") String filename,
4141
@RequestParam("nbResponsesByFile") int nbSU)
4242
throws Exception {
43-
log.info("Split XML file : {} into {} SU by file", filename, nbSU);
4443
XMLSplitter.split(inputFolder, filename, outputFolder, "SurveyUnit", nbSU);
4544
return ResponseEntity.ok("File split");
4645
}

0 commit comments

Comments
 (0)