Skip to content

Commit

Permalink
#76 [feat] 폼 데이터 형식일 때 body 값 로깅 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
KWY0218 committed Jan 10, 2024
1 parent 5883d6d commit f4c7ae5
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/main/java/com/moddy/server/config/aspect/LoggingAspect.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.moddy.server.config.aspect;

import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.Part;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
Expand All @@ -24,6 +26,7 @@ public class LoggingAspect {
private final ObjectMapper objectMapper = new ObjectMapper();
private static final String START_LOG = "================================================NEW===============================================\n";
private static final String END_LOG = "================================================END===============================================\n";
private static final String MULTI_PART_FORM_DATA = "multipart/form-data";

@Pointcut("execution(* com.moddy.server.controller..*(..)) || ( execution(* com.moddy.server.common.exception..*(..)) && !execution(* com.moddy.server.common.exception.GlobalControllerExceptionAdvice.handleException*(..)))")
public void controllerInfoLevelExecute() {
Expand Down Expand Up @@ -63,12 +66,19 @@ private String getCommunicationData(
long startAt,
long endAt,
Object returnValue
) throws IOException {
) throws IOException, ServletException {
StringBuilder sb = new StringBuilder();

sb.append(START_LOG);
sb.append(String.format("====> Request: %s %s ({%d}ms)\n====> *Header = {%s}\n", request.getMethod(), request.getRequestURL(), endAt - startAt, getHeaders(request)));
if ("POST".equalsIgnoreCase(request.getMethod())) {
if ("POST".equalsIgnoreCase(request.getMethod()) && request.getContentType().contains(MULTI_PART_FORM_DATA)) {
sb.append("====> Body: ");
cachingRequest.getParts().stream().map(Part::getName).forEach(n -> {
String keyValue = String.format("%s = %s", n, request.getParameter(n));
sb.append(keyValue).append(", ");
});
sb.append("\n");
} else if ("POST".equalsIgnoreCase(request.getMethod())) {
sb.append(String.format("====> Body: {%s}\n", objectMapper.readTree(cachingRequest.getContentAsByteArray())));
}
if (returnValue != null) {
Expand Down

0 comments on commit f4c7ae5

Please sign in to comment.