-
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.
feat: change logging interceptor to filter to wrap request
- Loading branch information
1 parent
f9e54c0
commit 35e9d24
Showing
6 changed files
with
72 additions
and
52 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
58 changes: 58 additions & 0 deletions
58
src/main/kotlin/com/group4/ticketingservice/filter/LogFilter.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,58 @@ | ||
package com.group4.ticketingservice.filter | ||
|
||
import com.group4.ticketingservice.utils.logging.logger | ||
import jakarta.servlet.Filter | ||
import jakarta.servlet.FilterChain | ||
import jakarta.servlet.ServletRequest | ||
import jakarta.servlet.ServletResponse | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.slf4j.MDC | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.web.util.ContentCachingRequestWrapper | ||
import java.net.URLEncoder | ||
import java.util.UUID | ||
|
||
class LogFilter : Filter { | ||
|
||
private val log = logger() | ||
|
||
override fun doFilter(request: ServletRequest, response: ServletResponse, chain: FilterChain) { | ||
val httpServletRequest = ContentCachingRequestWrapper(request as HttpServletRequest) | ||
|
||
MDC.put("requestId", UUID.randomUUID().toString()) | ||
|
||
val queryParams = getQueryParams(request) | ||
val requestInfo = "[${request.method}] ${request.requestURI}$queryParams" | ||
|
||
log.info("{} userId : {}", requestInfo, getUserId()) | ||
|
||
chain.doFilter(httpServletRequest, response) | ||
|
||
var reqContent = String(httpServletRequest.contentAsByteArray) | ||
reqContent = reqContent.replace("\n", "").replace("\r", "") | ||
|
||
if (reqContent != "") { | ||
reqContent = "RequestBody : $reqContent" | ||
log.info("{}", reqContent) | ||
} | ||
MDC.remove("requestId") | ||
} | ||
|
||
private fun getUserId() = SecurityContextHolder.getContext().authentication.principal.toString() | ||
|
||
private fun getQueryParams(request: HttpServletRequest): String { | ||
val paramMap = request.parameterMap | ||
val params = StringBuilder() | ||
if (paramMap.isNotEmpty()) params.append("?") | ||
for ((key, value1) in paramMap) { | ||
if (params.isNotEmpty()) params.append("&") | ||
for (value in value1) { | ||
params.append(URLEncoder.encode(key, "UTF-8")) | ||
params.append("=") | ||
params.append(URLEncoder.encode(value, "UTF-8")) | ||
} | ||
} | ||
|
||
return params.toString() | ||
} | ||
} |
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
46 changes: 0 additions & 46 deletions
46
src/main/kotlin/com/group4/ticketingservice/utils/logging/LogInterceptor.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