-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into feature/#110
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
39 changes: 39 additions & 0 deletions
39
server/src/main/java/com/talkka/server/common/log/HttpLogMessage.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,39 @@ | ||
package com.talkka.server.common.log; | ||
|
||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
import org.springframework.web.util.ContentCachingResponseWrapper; | ||
|
||
import lombok.Builder; | ||
|
||
@Builder | ||
public record HttpLogMessage(String httpMethod, String requestUri, Integer httpStatus, String clientIp, | ||
Long elapsedTimeMs, String requestParam, String requestBody, String responseBody) { | ||
public static HttpLogMessage create(ContentCachingRequestWrapper request, ContentCachingResponseWrapper response, | ||
Long elapsedTimeMs) { | ||
return HttpLogMessage.builder() | ||
.httpMethod(request.getMethod()) | ||
.requestUri(request.getRequestURI()) | ||
.httpStatus(response.getStatus()) | ||
.clientIp(request.getRemoteAddr()) | ||
.elapsedTimeMs(elapsedTimeMs) | ||
.requestParam(request.getQueryString()) | ||
.requestBody(new String(request.getContentAsByteArray())) | ||
.responseBody(new String(response.getContentAsByteArray())) | ||
.build(); | ||
} | ||
|
||
public String beatify() { | ||
String format = """ | ||
HTTP REQUEST %s %s %s | ||
| ClientIp: %s | ||
| ElapsedTimeMs: %sms | ||
| RequestParam: %s | ||
| RequestBody: %s | ||
| ResponseBody: %s | ||
"""; | ||
|
||
return String.format(format, httpMethod, requestUri, httpStatus, clientIp, elapsedTimeMs, requestParam, | ||
requestBody, responseBody | ||
); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
server/src/main/java/com/talkka/server/common/log/LoggingFilter.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,43 @@ | ||
package com.talkka.server.common.log; | ||
|
||
import java.io.IOException; | ||
|
||
import org.springframework.core.Ordered; | ||
import org.springframework.core.annotation.Order; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import org.springframework.web.util.ContentCachingRequestWrapper; | ||
import org.springframework.web.util.ContentCachingResponseWrapper; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
|
||
@Slf4j | ||
@Component | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
public class LoggingFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, | ||
FilterChain filterChain) throws ServletException, IOException { | ||
|
||
var cachingRequestWrapper = new ContentCachingRequestWrapper(request); | ||
var cachingResponseWrapper = new ContentCachingResponseWrapper(response); | ||
|
||
var startTime = System.currentTimeMillis(); | ||
filterChain.doFilter(cachingRequestWrapper, cachingResponseWrapper); | ||
var endTime = System.currentTimeMillis(); | ||
|
||
try { | ||
var logMessage = HttpLogMessage.create(cachingRequestWrapper, cachingResponseWrapper, | ||
(endTime - startTime)) | ||
.beatify(); | ||
log.info(logMessage); | ||
cachingResponseWrapper.copyBodyToResponse(); | ||
} catch (Exception exception) { | ||
log.error("Logging 실패"); | ||
} | ||
} | ||
} |