Skip to content

Commit cea6119

Browse files
authored
Logback을 Log4j2로 마이그레이션한다. (#117)
* feat: migrate from Logback to Log4j2 * feat: add Kotlin logging * fix: resolve dependency
1 parent 8ebf365 commit cea6119

File tree

17 files changed

+87
-93
lines changed

17 files changed

+87
-93
lines changed

api/build.gradle.kts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@ plugins {
77
alias(libs.plugins.jib)
88
}
99

10+
configurations {
11+
all {
12+
exclude("org.springframework.boot", "spring-boot-starter-logging")
13+
}
14+
}
15+
1016
dependencies {
1117
implementation(project(":domain"))
1218
implementation(project(":common:util"))
1319
implementation(project(":infrastructure:aws"))
1420
implementation(project(":infrastructure:oauth"))
1521
implementation(project(":storage:rdb"))
1622
implementation(project(":storage:redis"))
23+
implementation(project(":common:logging"))
1724

1825
implementation(libs.spring.boot.starter.web)
1926
implementation(libs.spring.boot.starter.aop)
2027
implementation(libs.spring.boot.starter.validation)
2128
implementation(libs.spring.boot.starter.security)
29+
implementation(libs.spring.boot.starter.log4j2)
30+
implementation(libs.jackson.yaml)
2231
implementation(libs.jakarta.validation)
2332
implementation(libs.jjwt.api)
2433
runtimeOnly(libs.jjwt.jackson)
2534
runtimeOnly(libs.jjwt.impl)
26-
runtimeOnly(project(":common:logging"))
2735

2836
testImplementation(testFixtures(project(":domain")))
2937
testImplementation(libs.bundles.spring.test)
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.gotchai.api.global.exception
22

3-
import com.gotchai.common.util.logger
3+
import com.gotchai.common.util.getLogger
44
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler
55
import java.lang.reflect.Method
66

77
class AsyncExceptionHandler : AsyncUncaughtExceptionHandler {
8-
private val log by logger()
8+
companion object {
9+
private val log = getLogger()
10+
}
911

1012
override fun handleUncaughtException(
11-
e: Throwable,
13+
ex: Throwable,
1214
method: Method,
1315
vararg params: Any?
1416
) {
15-
log.error("Exception : {}", e.message, e)
17+
log.error(ex) { "Exception : ${ex.message}" }
1618
}
1719
}

api/src/main/kotlin/com/gotchai/api/global/exception/GlobalExceptionHandler.kt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.gotchai.api.global.exception
22

33
import com.gotchai.api.global.dto.ApiResponse
44
import com.gotchai.api.global.dto.ErrorResponse
5-
import com.gotchai.common.util.logger
5+
import com.gotchai.common.util.getLogger
66
import com.gotchai.domain.global.exception.ServerException
77
import jakarta.validation.ConstraintViolationException
88
import org.springframework.http.HttpHeaders
@@ -20,7 +20,7 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExcep
2020
@RestControllerAdvice(basePackages = ["com.gotchai"])
2121
class GlobalExceptionHandler : ResponseEntityExceptionHandler() {
2222
companion object {
23-
private val log by logger()
23+
private val log = getLogger()
2424
}
2525

2626
override fun handleMethodArgumentNotValid(
@@ -29,7 +29,7 @@ class GlobalExceptionHandler : ResponseEntityExceptionHandler() {
2929
status: HttpStatusCode,
3030
request: WebRequest
3131
): ResponseEntity<Any> {
32-
log.error("MethodArgumentNotValidException : {}", ex.message, ex)
32+
log.error(ex) { "MethodArgumentNotValidException : ${ex.message}" }
3333

3434
val message =
3535
ex.bindingResult.allErrors
@@ -50,7 +50,7 @@ class GlobalExceptionHandler : ResponseEntityExceptionHandler() {
5050

5151
@ExceptionHandler(ConstraintViolationException::class)
5252
fun handleConstraintViolationException(ex: ConstraintViolationException): ResponseEntity<ApiResponse<ErrorResponse>> {
53-
log.error("ConstraintViolationException: {}", ex.message, ex)
53+
log.error(ex) { "ConstraintViolationException: ${ex.message}" }
5454
val violations =
5555
ex.constraintViolations.associate {
5656
it.propertyPath.toString().substringAfterLast(".", "unknown") to it.message
@@ -69,7 +69,7 @@ class GlobalExceptionHandler : ResponseEntityExceptionHandler() {
6969

7070
@ExceptionHandler(MethodArgumentTypeMismatchException::class)
7171
fun handleMethodArgumentTypeMismatchException(ex: MethodArgumentTypeMismatchException): ResponseEntity<ApiResponse<ErrorResponse>> {
72-
log.error("MethodArgumentTypeMismatchException : {}", ex.message, ex)
72+
log.error(ex) { "MethodArgumentTypeMismatchException : ${ex.message}" }
7373

7474
val errorResponse =
7575
ErrorResponse(
@@ -89,7 +89,7 @@ class GlobalExceptionHandler : ResponseEntityExceptionHandler() {
8989
status: HttpStatusCode,
9090
request: WebRequest
9191
): ResponseEntity<Any> {
92-
log.error("HttpRequestMethodNotSupportedException : {}", ex.message, ex)
92+
log.error(ex) { "HttpRequestMethodNotSupportedException : ${ex.message}" }
9393

9494
val errorResponse =
9595
ErrorResponse(
@@ -105,7 +105,7 @@ class GlobalExceptionHandler : ResponseEntityExceptionHandler() {
105105

106106
@ExceptionHandler(ServerException::class)
107107
fun handleServerException(ex: ServerException): ResponseEntity<ApiResponse<ErrorResponse>> {
108-
log.error("gotchai CustomException : {}", ex.message, ex)
108+
log.error(ex) { "gotchai CustomException : ${ex.message}" }
109109

110110
val errorResponse =
111111
ErrorResponse(
@@ -121,7 +121,7 @@ class GlobalExceptionHandler : ResponseEntityExceptionHandler() {
121121

122122
@ExceptionHandler(Exception::class)
123123
protected fun handleException(ex: Exception): ResponseEntity<ApiResponse<ErrorResponse>> {
124-
log.error("Internal Server Error : {}", ex.message, ex)
124+
log.error(ex) { "Internal Server Error : ${ex.message}" }
125125

126126
val errorResponse =
127127
ErrorResponse(

api/src/main/kotlin/com/gotchai/api/global/logging/LoggingFilter.kt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,33 @@ package com.gotchai.api.global.logging
33
import com.fasterxml.jackson.databind.JsonNode
44
import com.fasterxml.jackson.databind.ObjectMapper
55
import com.fasterxml.jackson.databind.node.ObjectNode
6+
import com.gotchai.common.util.getLogger
67
import jakarta.servlet.FilterChain
78
import jakarta.servlet.http.HttpServletRequest
89
import jakarta.servlet.http.HttpServletResponse
9-
import org.slf4j.LoggerFactory
10+
import org.slf4j.MDC
1011
import org.springframework.stereotype.Component
1112
import org.springframework.web.filter.OncePerRequestFilter
1213
import org.springframework.web.util.ContentCachingRequestWrapper
1314
import org.springframework.web.util.WebUtils
1415
import java.io.IOException
1516
import java.io.UnsupportedEncodingException
17+
import java.util.*
1618

1719
@Component
1820
class LoggingFilter(
1921
private val objectMapper: ObjectMapper
2022
) : OncePerRequestFilter() {
2123
companion object {
22-
private val log by lazy { LoggerFactory.getLogger(this::class.java) }
24+
private val log = getLogger()
2325
}
2426

2527
override fun doFilterInternal(
2628
request: HttpServletRequest,
2729
response: HttpServletResponse,
2830
filterChain: FilterChain
2931
) {
32+
MDC.put("traceId", UUID.randomUUID().toString().substring(0..7))
3033
val isFirstRequest = !this.isAsyncDispatch(request)
3134
var wrapper = request
3235
if (isFirstRequest && request !is ContentCachingRequestWrapper) {
@@ -49,7 +52,7 @@ class LoggingFilter(
4952
setParameters(request, logData)
5053
setPayload(request, logData)
5154
val json = objectMapper.writeValueAsString(logData)
52-
log.info("REQUEST : $json")
55+
log.info { "REQUEST : $json" }
5356
}
5457

5558
private fun setParameters(

api/src/main/kotlin/com/gotchai/api/global/logging/LoggingStopWatchAdvice.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
package com.gotchai.api.global.logging
22

3+
import com.gotchai.common.util.getLogger
34
import org.aspectj.lang.ProceedingJoinPoint
45
import org.aspectj.lang.annotation.Around
56
import org.aspectj.lang.annotation.Aspect
6-
import org.slf4j.Logger
7-
import org.slf4j.LoggerFactory
87
import org.springframework.stereotype.Component
98
import org.springframework.web.context.request.RequestContextHolder
109
import org.springframework.web.context.request.ServletRequestAttributes
@@ -13,8 +12,8 @@ import org.springframework.web.context.request.ServletRequestAttributes
1312
@Component
1413
class LoggingStopWatchAdvice {
1514
companion object {
16-
val log: Logger = LoggerFactory.getLogger(this::class.java)
17-
const val MAX_AFFORDABLE_TIME: Long = 3000
15+
private val log = getLogger()
16+
const val MAX_AFFORDABLE_TIME = 3000
1817
}
1918

2019
@Around("execution(* com.gotchai.api.presentation..*Controller.*(..))")
@@ -28,15 +27,12 @@ class LoggingStopWatchAdvice {
2827
val methodName = joinPoint.signature.name
2928

3029
if (timeMs > MAX_AFFORDABLE_TIME) {
31-
log.warn(
32-
"method=${getMethod()}, url=${getRequestURI()}, call: $className - $methodName - timeMs:${timeMs}ms"
33-
)
30+
log.warn { "method=${getMethod()}, url=${getRequestURI()}, call: $className - $methodName - timeMs:${timeMs}ms" }
31+
3432
return proceed
3533
}
3634

37-
log.info(
38-
"method=${getMethod()}, url=${getRequestURI()}, call: $className - $methodName - timeMs:${timeMs}ms"
39-
)
35+
log.info { "method=${getMethod()}, url=${getRequestURI()}, call: $className - $methodName - timeMs:${timeMs}ms" }
4036

4137
return proceed
4238
}

common/logging/build.gradle.kts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +0,0 @@
1-
dependencies {
2-
implementation(libs.micrometer.tracing.bridge.brave)
3-
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
logging:
2-
config: classpath:logback/logback-${SPRING_PROFILES_ACTIVE:local}.xml
2+
config: classpath:log4j2-${SPRING_PROFILES_ACTIVE}.yml
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Configuration:
2+
name: Default
3+
status: info
4+
Appenders:
5+
Console:
6+
name: Console_Appender
7+
target: SYSTEM_OUT
8+
PatternLayout:
9+
pattern: "%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{[%p]} %clr{%c{1.}}{cyan}%clr{:}{faint}%equals{ [%X{traceId}]}{ []}{} %m%n%xwEx"
10+
Loggers:
11+
Root:
12+
level: info
13+
AppenderRef:
14+
- ref: Console_Appender
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Configuration:
2+
name: Default
3+
status: info
4+
Appenders:
5+
Console:
6+
name: Console_Appender
7+
target: SYSTEM_OUT
8+
PatternLayout:
9+
pattern: "%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{[%p]} %clr{%c{1.}}{cyan}%clr{:}{faint}%equals{ [%X{traceId}]}{ []}{} %m%n%xwEx"
10+
Loggers:
11+
Root:
12+
level: info
13+
AppenderRef:
14+
- ref: Console_Appender
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Configuration:
2+
name: Default
3+
status: info
4+
Appenders:
5+
Console:
6+
name: Console_Appender
7+
target: SYSTEM_OUT
8+
PatternLayout:
9+
pattern: "%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{[%p]} %clr{%c{1.}}{cyan}%clr{:}{faint}%equals{ [%X{traceId}]}{ []}{} %m%n%xwEx"
10+
Loggers:
11+
Root:
12+
level: info
13+
AppenderRef:
14+
- ref: Console_Appender

0 commit comments

Comments
 (0)