-
Notifications
You must be signed in to change notification settings - Fork 5
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
[feat] log filter 추가 및 aop 수정 #39
Merged
linglong67
merged 2 commits into
Kernel360:develop
from
linglong67:feature/log-filter-and-aop
Jan 10, 2024
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
11 changes: 11 additions & 0 deletions
11
module-api/src/main/java/com/kernel360/global/annotation/LogExecutionTime.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.kernel360.global.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface LogExecutionTime { | ||
} |
24 changes: 24 additions & 0 deletions
24
module-api/src/main/java/com/kernel360/global/aop/LogAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.kernel360.global.aop; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Slf4j | ||
@Aspect | ||
@Component | ||
public class LogAspect { | ||
|
||
@Around("@annotation(com.kernel360.global.annotation.LogExecutionTime)") | ||
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable { | ||
long start = System.currentTimeMillis(); | ||
Object proceed = joinPoint.proceed(); | ||
|
||
long executionTime = System.currentTimeMillis() - start; | ||
log.info("##### @LogExecutionTime ##### " + joinPoint.getSignature() + " executed in " + executionTime + "ms"); | ||
|
||
return proceed; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
module-api/src/main/java/com/kernel360/global/filter/LogFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.kernel360.global.filter; | ||
|
||
import jakarta.servlet.*; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
import org.springframework.web.util.ContentCachingResponseWrapper; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
@Component | ||
public class LogFilter implements Filter { | ||
|
||
@Override | ||
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { | ||
ContentCachingRequestWrapper request = new ContentCachingRequestWrapper((HttpServletRequest) req); | ||
ContentCachingResponseWrapper response = new ContentCachingResponseWrapper((HttpServletResponse) res); | ||
log.info("##### INIT URI: {}", request.getRequestURI()); | ||
|
||
chain.doFilter(request, response); | ||
|
||
// Request | ||
StringBuilder requestHeaderValues = new StringBuilder(); | ||
request.getHeaderNames().asIterator().forEachRemaining(headerKey -> { | ||
String headerValue = request.getHeader(headerKey); | ||
|
||
requestHeaderValues | ||
.append("[") | ||
.append(headerKey) | ||
.append(" : ") | ||
.append(headerValue) | ||
.append("] "); | ||
}); | ||
|
||
String requestBody = new String(request.getContentAsByteArray()); | ||
String uri = request.getRequestURI(); | ||
String method = request.getMethod(); | ||
log.info("##### REQUEST ##### uri: {}, method: {}, header: {}, body: {}", uri, method, requestHeaderValues, requestBody); | ||
|
||
// Response | ||
StringBuilder responseHeaderValues = new StringBuilder(); | ||
response.getHeaderNames().forEach(headerKey -> { | ||
String headerValue = response.getHeader(headerKey); | ||
|
||
responseHeaderValues | ||
.append("[") | ||
.append(headerKey) | ||
.append(" : ") | ||
.append(headerValue) | ||
.append("] "); | ||
}); | ||
|
||
String responseBody = new String(response.getContentAsByteArray()); | ||
log.info("##### RESPONSE ##### uri: {}, method: {}, header: {}, body: {}", uri, method, responseHeaderValues, responseBody); | ||
|
||
response.copyBodyToResponse(); | ||
} | ||
Comment on lines
+18
to
+60
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. 제가 이건 리팩토링 고려해보고 수정하게 되면 다음에 다시 올릴게요~! |
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
특정 로그를 남기도록 커스텀 어노테이션으로 추가하고 싶다면 여기 LogAspect.java 에 메서드를 추가하고 어노테이션을 선언하면 되나요?
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.
네! 맞습니다~!
커스텀 어노테이션 생성 후에 위 코드처럼 메소드 추가해주시면 되요~