-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BE] 요청 로그에 사용자 정보를 추가 #298
Changes from 3 commits
0c203e0
4942a3b
900eb58
5c2df39
a6702fd
98dea92
b6beff9
ccb5489
539d9c3
3f23c48
12be148
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,28 @@ | ||
package kr.momo.config; | ||
|
||
import java.util.List; | ||
import kr.momo.config.filter.LogInterceptor; | ||
import kr.momo.controller.auth.AuthArgumentResolver; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.web.method.support.HandlerMethodArgumentResolver; | ||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; | ||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; | ||
|
||
@Configuration | ||
@RequiredArgsConstructor | ||
public class WebConfig implements WebMvcConfigurer { | ||
|
||
private final AuthArgumentResolver authArgumentResolver; | ||
private final LogInterceptor logInterceptor; | ||
|
||
@Override | ||
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) { | ||
resolvers.add(authArgumentResolver); | ||
} | ||
|
||
@Override | ||
public void addInterceptors(InterceptorRegistry registry) { | ||
registry.addInterceptor(logInterceptor).addPathPatterns("/api/v1/**"); | ||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,51 @@ | ||
package kr.momo.config.filter; | ||
|
||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import java.util.Arrays; | ||
import java.util.Optional; | ||
import kr.momo.service.auth.JwtManager; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class LogGenerator { | ||
|
||
private static final String ANONYMOUS = "ANONYMOUS"; | ||
private static final String ACCESS_TOKEN = "ACCESS_TOKEN"; | ||
|
||
private final JwtManager jwtManager; | ||
|
||
public void logRequest(String traceId, HttpServletRequest request) { | ||
String httpMethod = request.getMethod(); | ||
String requestURI = request.getRequestURI(); | ||
String remoteAddr = request.getRemoteAddr(); | ||
Cookie[] cookies = request.getCookies(); | ||
String userInfo = getCookieValue(cookies).orElse(ANONYMOUS); | ||
|
||
if (isLoginUser(userInfo)) { | ||
userInfo = String.valueOf(jwtManager.extract(userInfo)); | ||
} | ||
|
||
log.info("REQUEST [{}][USERID:{}][{} {}]", traceId, userInfo, httpMethod, requestURI); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 응답 로그에도 사용자 정보를 남기면 좋을 것 같은데, 요청 로그에만 명시한 이유가 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 요청에 사용자 정보를 남겨 두었으니, 응답 로그는 pr 본문을 예로 들면, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 응답 로그만 확인했을 때도 정보가 명확했으면 좋겠어요 😊 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 동의합니다. 방대한 양의 로그를 한 번 찾는 것도 수고로운데, 다른 로그를 다시 찾아봐야 한다면 오버헤드가 더 클 것 같아요 |
||
} | ||
|
||
private Optional<String> getCookieValue(Cookie[] cookies) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 이 로직은 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 네 맞습니다. 어색하다고 느꼈는데, 인터셉터를 분리하는 방향으로 해결할 수 있겠네요!
위 내용을 반영해서 수정했습니다~ |
||
if (cookies == null) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Arrays.stream(cookies) | ||
.filter(cookie -> ACCESS_TOKEN.equals(cookie.getName())) | ||
.map(Cookie::getValue) | ||
.findFirst(); | ||
} | ||
|
||
log.info("REQUEST [{}][{} {}][{}]", traceId, httpMethod, requestURI, remoteAddr); | ||
private boolean isLoginUser(String token) { | ||
return !ANONYMOUS.equals(token); | ||
} | ||
|
||
public void logResponse(String traceId, long duration, HttpServletRequest request, HttpServletResponse response) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package kr.momo.config.filter; | ||
|
||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.MDC; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.servlet.HandlerInterceptor; | ||
|
||
@Slf4j | ||
@RequiredArgsConstructor | ||
@Component | ||
public class LogInterceptor implements HandlerInterceptor { | ||
|
||
public static final String TRACE_ID = "traceId"; | ||
|
||
private final TraceIdGenerator traceIdGenerator; | ||
private final LogGenerator logGenerator; | ||
|
||
@Override | ||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { | ||
String traceId = traceIdGenerator.generateShortUuid(); | ||
MDC.put(TRACE_ID, traceId); | ||
logGenerator.logRequest(traceId, request); | ||
request.setAttribute("startTime", System.currentTimeMillis()); | ||
return true; | ||
} | ||
|
||
@Override | ||
public void afterCompletion( | ||
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex | ||
) { | ||
long startTime = (Long) request.getAttribute("startTime"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 하드 코딩된 |
||
long duration = System.currentTimeMillis() - startTime; | ||
String traceId = MDC.get(TRACE_ID); | ||
logGenerator.logResponse(traceId, duration, request, response); | ||
MDC.remove(TRACE_ID); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
패키지 이름도 수정하면 좋을 것 같아요!!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
동의합니다!