-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/DRAW-240' into sandbox
- Loading branch information
Showing
12 changed files
with
161 additions
and
92 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
log/ | ||
/log | ||
.gradle | ||
build/ | ||
!gradle/wrapper/gradle-wrapper.jar | ||
|
60 changes: 29 additions & 31 deletions
60
app/api/src/main/kotlin/com/xorker/draw/log/ApiLoggingFilter.kt
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 |
---|---|---|
@@ -1,63 +1,61 @@ | ||
package com.xorker.draw.log | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.xorker.draw.support.logging.defaultApiJsonMap | ||
import com.xorker.draw.support.logging.logger | ||
import com.xorker.draw.support.logging.registerRequestId | ||
import jakarta.servlet.Filter | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.ServletRequest | ||
import jakarta.servlet.ServletResponse | ||
import jakarta.servlet.annotation.WebFilter | ||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.slf4j.MDC | ||
import org.springframework.core.Ordered | ||
import org.springframework.core.annotation.Order | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.util.ContentCachingRequestWrapper | ||
import org.springframework.web.util.ContentCachingResponseWrapper | ||
|
||
@Component | ||
@WebFilter(urlPatterns = ["/api/*"]) | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
class ApiLoggingFilter : Filter { | ||
class ApiLoggingFilter( | ||
private val objectMapper: ObjectMapper, | ||
) : Filter { | ||
private val logger = logger() | ||
|
||
override fun doFilter(servletRequest: ServletRequest, servletResponse: ServletResponse, chain: FilterChain) { | ||
val request = RequestLoggingWrapper(servletRequest as HttpServletRequest) | ||
val response = getResponseWrapper(servletResponse as HttpServletResponse) | ||
registerRequestId() | ||
val request = ContentCachingRequestWrapper(servletRequest as HttpServletRequest) | ||
val response = ContentCachingResponseWrapper(servletResponse as HttpServletResponse) | ||
|
||
try { | ||
chain.doFilter(request, response) | ||
} finally { | ||
logger.info("${generateRequestLog(request)}\n${generateResponseLog(response)}") | ||
logger.info(generateLog(request, response)) | ||
response.copyBodyToResponse() | ||
} | ||
} | ||
|
||
private fun generateRequestLog(request: HttpServletRequest): String { | ||
val log = StringBuilder("[Req]\n${request.method} ${request.requestURI}") | ||
private fun generateLog(request: ContentCachingRequestWrapper, response: ContentCachingResponseWrapper): String { | ||
val data = defaultApiJsonMap( | ||
// Request 부분 | ||
"method" to request.method, | ||
"uri" to request.requestURI, | ||
"query" to request.queryString, | ||
"header" to request.headerNames.asSequence().map { it to request.getHeader(it) }.toMap(), | ||
"requestBody" to request.contentAsString, | ||
|
||
if (request.queryString.isNullOrBlank().not()) { | ||
log.append("?${request.queryString}") | ||
} | ||
|
||
request.headerNames.iterator().forEach { | ||
log.append("\n$it=${request.getHeader(it)}") | ||
} | ||
|
||
request.reader.lines().forEach { | ||
log.append("\n$it") | ||
} | ||
// Response 부분 | ||
"status" to response.status, | ||
"responseBody" to String(response.contentAsByteArray), | ||
|
||
return log.toString() | ||
} | ||
|
||
private fun generateResponseLog(response: ContentCachingResponseWrapper): String { | ||
val log = StringBuilder("[Res]\n${response.status}") | ||
|
||
val responseCacheWrapperObject = getResponseWrapper(response) | ||
"requestId" to MDC.get("requestId"), | ||
"userId" to MDC.get("userId"), | ||
) | ||
|
||
val responseStr = String(responseCacheWrapperObject.contentAsByteArray) | ||
|
||
log.append("\n$responseStr") | ||
return log.toString() | ||
return objectMapper.writeValueAsString(data) | ||
} | ||
|
||
private fun getResponseWrapper(response: HttpServletResponse): ContentCachingResponseWrapper = | ||
response as? ContentCachingResponseWrapper ?: ContentCachingResponseWrapper(response) | ||
} |
55 changes: 0 additions & 55 deletions
55
app/api/src/main/kotlin/com/xorker/draw/log/RequestLoggingWrapper.kt
This file was deleted.
Oops, something went wrong.
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
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
74 changes: 74 additions & 0 deletions
74
app/websocket/src/main/kotlin/com/xorker/draw/websocket/log/WebSocketLogger.kt
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,74 @@ | ||
package com.xorker.draw.websocket.log | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.xorker.draw.support.logging.defaultApiJsonMap | ||
import com.xorker.draw.support.logging.logger | ||
import com.xorker.draw.support.logging.registerRequestId | ||
import com.xorker.draw.websocket.SessionId | ||
import com.xorker.draw.websocket.SessionUseCase | ||
import com.xorker.draw.websocket.message.request.dto.WebSocketRequest | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.socket.CloseStatus | ||
import org.springframework.web.socket.WebSocketSession | ||
|
||
@Component | ||
class WebSocketLogger( | ||
private val sessionUseCase: SessionUseCase, | ||
private val objectMapper: ObjectMapper, | ||
) { | ||
private val logger = logger() | ||
|
||
fun afterConnectionEstablished(session: WebSocketSession) { | ||
logger.info( | ||
objectMapper.writeValueAsString( | ||
defaultApiJsonMap( | ||
"action" to "WS_CONNECT", | ||
"sessionId" to session.id, | ||
), | ||
), | ||
) | ||
} | ||
|
||
fun handleRequest(session: WebSocketSession, request: WebSocketRequest, origin: (WebSocketSession, WebSocketRequest) -> Unit) { | ||
registerRequestId() | ||
try { | ||
origin(session, request) | ||
} finally { | ||
val log = generateLog(SessionId(session.id), request) | ||
logger.info(log) | ||
} | ||
} | ||
|
||
private fun generateLog(sessionId: SessionId, request: WebSocketRequest): String { | ||
val data = defaultApiJsonMap( | ||
"action" to request.action, | ||
"requestBody" to request.body, | ||
"sessionId" to sessionId.value, | ||
) | ||
|
||
val session = sessionUseCase.getSession(sessionId) | ||
if (session != null) { | ||
data["userId"] = session.user.id.value | ||
data["roomId"] = session.roomId | ||
} | ||
|
||
return objectMapper.writeValueAsString(data) | ||
} | ||
|
||
fun afterConnectionClosed(session: WebSocketSession, status: CloseStatus) { | ||
val data = defaultApiJsonMap( | ||
"action" to "WS_CLOSED", | ||
"status" to status, | ||
"sessionId" to session.id, | ||
) | ||
|
||
val sessionDto = sessionUseCase.getSession(SessionId(session.id)) | ||
if (sessionDto != null) { | ||
data["userId"] = sessionDto.user.id.value | ||
data["roomId"] = sessionDto.roomId | ||
} | ||
|
||
val log = objectMapper.writeValueAsString(data) | ||
logger.info(log) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
support/logging/src/main/kotlin/com/xorker/draw/support/logging/Logger.kt
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 |
---|---|---|
@@ -1,6 +1,25 @@ | ||
package com.xorker.draw.support.logging | ||
|
||
import java.time.LocalDateTime | ||
import java.time.format.DateTimeFormatter | ||
import java.util.UUID | ||
import org.slf4j.Logger | ||
import org.slf4j.LoggerFactory | ||
import org.slf4j.MDC | ||
|
||
inline fun <reified T> T.logger(): Logger = LoggerFactory.getLogger(T::class.java) | ||
|
||
fun registerRequestId() { | ||
MDC.put("requestId", UUID.randomUUID().toString()) | ||
} | ||
|
||
private val TIMESTAMP_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSXXX") | ||
fun defaultApiJsonMap(vararg pairs: Pair<String, Any?>): MutableMap<String, Any?> { | ||
val now = LocalDateTime.now() | ||
|
||
val map = mutableMapOf(*pairs) | ||
|
||
map["timestamp"] = TIMESTAMP_FORMATTER.format(now) | ||
|
||
return map | ||
} |
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
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
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
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
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