-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
165 additions
and
4 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
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
85 changes: 85 additions & 0 deletions
85
src/main/kotlin/backend/itracker/logging/aspect/LoggerAspect.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,85 @@ | ||
package backend.itracker.logging.aspect | ||
|
||
import com.google.common.net.HttpHeaders | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import jakarta.servlet.http.HttpServletRequest | ||
import org.aspectj.lang.JoinPoint | ||
import org.aspectj.lang.annotation.AfterReturning | ||
import org.aspectj.lang.annotation.Aspect | ||
import org.aspectj.lang.annotation.Before | ||
import org.aspectj.lang.annotation.Pointcut | ||
import org.slf4j.MDC | ||
import org.springframework.context.annotation.Profile | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.context.request.RequestContextHolder | ||
import org.springframework.web.context.request.ServletRequestAttributes | ||
import java.util.* | ||
|
||
|
||
private val LOGGER = KotlinLogging.logger {} | ||
private val IP_HEADERS = listOf( | ||
HttpHeaders.X_FORWARDED_FOR, | ||
"Proxy-Client-IP", | ||
"WL-Proxy-Client-IP", | ||
"HTTP_CLIENT_IP", | ||
"HTTP_X_FORWARDED_FOR" | ||
) | ||
|
||
@Profile("!test") | ||
@Component | ||
@Aspect | ||
class LoggerAspect { | ||
|
||
@Pointcut( | ||
""" @annotation(org.springframework.web.bind.annotation.PostMapping) || | ||
@annotation(org.springframework.web.bind.annotation.GetMapping) || | ||
@annotation(org.springframework.web.bind.annotation.PatchMapping) || | ||
@annotation(org.springframework.web.bind.annotation.PutMapping) || | ||
@annotation(org.springframework.web.bind.annotation.DeleteMapping)""" | ||
) | ||
fun apiInfo() { | ||
} | ||
|
||
@Before("apiInfo()") | ||
fun beforeApiRequest(joinPoint: JoinPoint) { | ||
val request = | ||
(Objects.requireNonNull(RequestContextHolder.getRequestAttributes()) as ServletRequestAttributes).request | ||
LOGGER.info { | ||
""" | ||
[ API Start ] | ||
- Request ID: ${getRequestId()} | ||
- Method: ${request.method} | ||
- URI: ${getURI(request)} | ||
- IP: ${getClientIP(request)} | ||
- Signature: ${getSignature(joinPoint)} | ||
""".trimIndent() | ||
} | ||
} | ||
|
||
@AfterReturning(value = "apiInfo()", returning = "response") | ||
fun afterApiRequest(response: Any) { | ||
LOGGER.info { | ||
""" | ||
[ API End ] | ||
- Request ID: ${getRequestId()} | ||
- Response: $response | ||
""".trimIndent() | ||
} | ||
} | ||
|
||
private fun getRequestId(): String { | ||
return MDC.get("request_id") | ||
} | ||
|
||
private fun getURI(request: HttpServletRequest): String { | ||
return "${request.requestURI}${request.queryString.let { if (it != null) "?$it" else "" }}" | ||
} | ||
|
||
private fun getClientIP(request: HttpServletRequest): String? { | ||
return IP_HEADERS.firstNotNullOfOrNull { header -> request.getHeader(header) } ?: request.remoteAddr | ||
} | ||
|
||
private fun getSignature(joinPoint: JoinPoint): String { | ||
return "${joinPoint.signature.declaringType.simpleName}.${joinPoint.signature.name}" | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/kotlin/backend/itracker/logging/filter/MDCLoggingFilter.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,26 @@ | ||
package backend.itracker.logging.filter | ||
|
||
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.core.Ordered | ||
import org.springframework.core.annotation.Order | ||
import org.springframework.stereotype.Component | ||
import java.util.* | ||
|
||
@Component | ||
@Order(Ordered.HIGHEST_PRECEDENCE) | ||
class MDCLoggingFilter : Filter { | ||
|
||
override fun doFilter(request: ServletRequest?, response: ServletResponse?, chain: FilterChain?) { | ||
val requestId = (request as HttpServletRequest).getHeader("X-Request-ID") | ||
MDC.put("request_id", requestId ?: UUID.randomUUID().toString()) | ||
|
||
chain!!.doFilter(request, response) | ||
|
||
MDC.clear() | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<Configuration status="INFO"> | ||
<Properties> | ||
<Property name="FILE_NAME">daily-log</Property> | ||
<Property name="WARN_FILE_NAME">warn-log</Property> | ||
<Property name="ERROR_FILE_NAME">error-log</Property> | ||
<Property name="BASE_DIR">logs</Property> | ||
<Property name="COLOR_PATTERN">%style{%d{ISO8601}}{white} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable</Property> | ||
<Property name="PATTERN">[%equals{%X{request_id}}{}{startup}] %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n</Property> | ||
</Properties> | ||
|
||
<Appenders> | ||
<Console name="console" target="SYSTEM_OUT" follow="true"> | ||
<PatternLayout pattern="${COLOR_PATTERN}" charset="UTF-8"/> | ||
</Console> | ||
|
||
<RollingFile name="dailyFile"> | ||
<FileName>${BASE_DIR}/${FILE_NAME}.log</FileName> | ||
<FilePattern>${BASE_DIR}/${FILE_NAME}-%d{yyyy-MM-dd}.%i.log.gz</FilePattern> | ||
<PatternLayout pattern="${PATTERN}" /> | ||
<Policies> | ||
<SizeBasedTriggeringPolicy size="50MB"/> | ||
<TimeBasedTriggeringPolicy interval="1" modulate="true"/> | ||
</Policies> | ||
<DefaultRolloverStrategy max="7" fileIndex="max"> | ||
<Delete basePath="${BASE_DIR}" maxDepth="1"> | ||
<IfLastModified age = "15d"/> | ||
</Delete> | ||
</DefaultRolloverStrategy> | ||
</RollingFile> | ||
</Appenders> | ||
|
||
<Loggers> | ||
<Root level="INFO" additivity="false"> | ||
<AppenderRef ref="console"/> | ||
<AppenderRef ref="dailyFile"/> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |