Skip to content

Commit

Permalink
feat: 로깅 시 UUID가 아닌 회원 번호가 기록되도록 변경 (#428)
Browse files Browse the repository at this point in the history
* feat: logging 시 memberId가 나오도록 기능 추가

* feat: logging 시 memberId 및 identifier가 함께 나오도록 변경

* refactor: lombok getter 적용
  • Loading branch information
fromitive authored Aug 22, 2024
1 parent b891ff5 commit 261e5eb
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zzang.chongdae.global.exception;

import com.zzang.chongdae.logging.config.CachedHttpServletResponseWrapper;
import com.zzang.chongdae.logging.domain.MemberIdentifier;
import com.zzang.chongdae.logging.dto.LoggingErrorResponse;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
Expand Down Expand Up @@ -96,6 +97,7 @@ public ResponseEntity<ErrorMessage> handle(Exception e, HttpServletRequest reque
ErrorMessage errorMessage = new ErrorMessage("서버 관리자에게 문의하세요");

String identifier = UUID.randomUUID().toString();
MemberIdentifier memberIdentifier = new MemberIdentifier(request.getCookies());
String httpMethod = request.getMethod();
String uri = request.getRequestURI();
String requestBody = new String(request.getInputStream().readAllBytes());
Expand All @@ -112,8 +114,16 @@ public ResponseEntity<ErrorMessage> handle(Exception e, HttpServletRequest reque
long endTime = System.currentTimeMillis();
String latency = endTime - startTime + "ms";

LoggingErrorResponse logResponse = new LoggingErrorResponse(identifier, httpMethod, uri, requestBody,
"500", responseBody, latency, stackTrace);
LoggingErrorResponse logResponse = new LoggingErrorResponse(
identifier,
memberIdentifier.getIdInfo(),
httpMethod,
uri,
requestBody,
"500",
responseBody,
latency,
stackTrace);
log.error(logResponse.toString());

return ResponseEntity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.zzang.chongdae.global.exception.MarketException;
import com.zzang.chongdae.logging.domain.HttpStatusCategory;
import com.zzang.chongdae.logging.domain.MemberIdentifier;
import com.zzang.chongdae.logging.dto.LoggingInfoFailResponse;
import com.zzang.chongdae.logging.dto.LoggingInfoSuccessResponse;
import com.zzang.chongdae.logging.dto.LoggingWarnResponse;
Expand Down Expand Up @@ -48,6 +49,7 @@ public void afterCompletion(HttpServletRequest request,
String latency = endTime - startTime + "ms";

String identifier = UUID.randomUUID().toString();
MemberIdentifier memberIdentifier = new MemberIdentifier(request.getCookies());
String httpMethod = request.getMethod();
String uri = request.getRequestURI();
String requestBody = new String(request.getInputStream().readAllBytes());
Expand All @@ -58,18 +60,38 @@ public void afterCompletion(HttpServletRequest request,

HttpStatusCategory statusCategory = HttpStatusCategory.fromStatusCode(cachedResponse.getStatus());
if (statusCategory == HttpStatusCategory.INFO_SUCCESS) {
LoggingInfoSuccessResponse logResponse = new LoggingInfoSuccessResponse(identifier, httpMethod, uri,
requestBody, statusCode, latency);
LoggingInfoSuccessResponse logResponse = new LoggingInfoSuccessResponse(
identifier,
memberIdentifier.getIdInfo(),
httpMethod,
uri,
requestBody,
statusCode,
latency);
log.info(logResponse.toString());
}
if (statusCategory == HttpStatusCategory.INFO_FAIL) {
LoggingInfoFailResponse logResponse = new LoggingInfoFailResponse(identifier, httpMethod, uri,
requestBody, statusCode, responseBody, latency);
LoggingInfoFailResponse logResponse = new LoggingInfoFailResponse(
identifier,
memberIdentifier.getIdInfo(),
httpMethod,
uri,
requestBody,
statusCode,
responseBody,
latency);
log.info(logResponse.toString());
}
if (statusCategory == HttpStatusCategory.WARN && ex instanceof MarketException) {
LoggingWarnResponse logResponse = new LoggingWarnResponse(identifier, httpMethod, uri,
requestBody, statusCode, responseBody, latency);
LoggingWarnResponse logResponse = new LoggingWarnResponse(
identifier,
memberIdentifier.getIdInfo(),
httpMethod,
uri,
requestBody,
statusCode,
responseBody,
latency);
log.warn(logResponse.toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.zzang.chongdae.logging.domain;

import jakarta.servlet.http.Cookie;
import java.util.Arrays;
import java.util.Base64;
import lombok.Getter;

@Getter
public class MemberIdentifier {

public static String ACCESS_TOKEN_NAME = "access_token";
public static String ID_NOT_FOUND_INFO = "Not Found";

private final String idInfo;

public MemberIdentifier(Cookie[] cookies) {
this.idInfo = buildIdInfo(cookies);
}

private String buildIdInfo(Cookie[] cookies) {
if (cookies == null) {
return ID_NOT_FOUND_INFO;
}
return Arrays.stream(cookies)
.filter(cookie -> ACCESS_TOKEN_NAME.equals(cookie.getName()))
.findFirst()
.map(this::getAccessTokenInfo)
.orElseGet(() -> ID_NOT_FOUND_INFO);
}

private String getAccessTokenInfo(Cookie cookie) {
String jwtToken = cookie.getValue();
String[] tokenParts = jwtToken.split("\\.");
if (tokenParts.length == 3) {
String payload = new String(Base64.getUrlDecoder().decode(tokenParts[1]));
return payload;
}
return ID_NOT_FOUND_INFO;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zzang.chongdae.logging.dto;

public record LoggingErrorResponse(String identifier,
String memberIdentifier,
String httpMethod,
String uri,
String requestBody,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zzang.chongdae.logging.dto;

public record LoggingInfoFailResponse(String identifier,
String memberIdentifier,
String httpMethod,
String uri,
String requestBody,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zzang.chongdae.logging.dto;

public record LoggingInfoSuccessResponse(String identifier,
String memberIdentifier,
String httpMethod,
String uri,
String requestBody,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.zzang.chongdae.logging.dto;

public record LoggingWarnResponse(String identifier,
String memberIdentifier,
String httpMethod,
String uri,
String requestBody,
Expand Down

0 comments on commit 261e5eb

Please sign in to comment.