From f36defec5d59e30936c60d7af4e8a58ee19a3fbb Mon Sep 17 00:00:00 2001 From: haeyonghahn Date: Fri, 8 Mar 2024 23:59:49 +0900 Subject: [PATCH 1/2] =?UTF-8?q?Feat:=20=EC=A7=84=EC=9E=85=EC=A0=90=20?= =?UTF-8?q?=EC=A0=95=EB=B3=B4=20=EC=A0=80=EC=9E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/MdcLoggingInterceptor.java | 34 +++++++++++++++++++ .../springconfig/web/WebMvcConfig.java | 14 ++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/main/java/com/dnd/gooding/springconfig/web/MdcLoggingInterceptor.java create mode 100644 src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java diff --git a/src/main/java/com/dnd/gooding/springconfig/web/MdcLoggingInterceptor.java b/src/main/java/com/dnd/gooding/springconfig/web/MdcLoggingInterceptor.java new file mode 100644 index 0000000..042e060 --- /dev/null +++ b/src/main/java/com/dnd/gooding/springconfig/web/MdcLoggingInterceptor.java @@ -0,0 +1,34 @@ +package com.dnd.gooding.springconfig.web; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import lombok.extern.slf4j.Slf4j; +import org.slf4j.MDC; +import org.springframework.web.method.HandlerMethod; +import org.springframework.web.servlet.HandlerInterceptor; + +@Slf4j +public class MdcLoggingInterceptor implements HandlerInterceptor { + + public static final String REQUEST_CONTROLLER_MDC_KEY = "handler"; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) + throws Exception { + if (handler instanceof HandlerMethod handlerMethod) { + String handlerName = handlerMethod.getBeanType().getSimpleName(); + String methodName = handlerMethod.getMethod().getName(); + String controllerInfo = handlerName + "." + methodName; + MDC.put(REQUEST_CONTROLLER_MDC_KEY, controllerInfo); + } + return true; + } + + /** MDC에 저장된 정보를 모두 초기화 한다. */ + @Override + public void afterCompletion( + HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) + throws Exception { + MDC.clear(); + } +} diff --git a/src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java b/src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java new file mode 100644 index 0000000..a225a1b --- /dev/null +++ b/src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java @@ -0,0 +1,14 @@ +package com.dnd.gooding.springconfig.web; + +import org.springframework.context.annotation.Configuration; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new MdcLoggingInterceptor()); + } +} From e1f21246e346a23a4bcd0a4bca1ec76f6358f7de Mon Sep 17 00:00:00 2001 From: haeyonghahn Date: Sat, 9 Mar 2024 02:05:11 +0900 Subject: [PATCH 2/2] =?UTF-8?q?Feat:=20=EB=A1=9C=EA=B7=B8=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 로그 포맷 수정 - 진입점 추가 Resolves: feat/#56 --- build.gradle | 2 + ...ceptor.java => MDCLoggingInterceptor.java} | 8 ++- .../springconfig/web/WebMvcConfig.java | 2 +- src/main/resources/logback-spring.xml | 54 +++++++++++++++++++ 4 files changed, 64 insertions(+), 2 deletions(-) rename src/main/java/com/dnd/gooding/springconfig/web/{MdcLoggingInterceptor.java => MDCLoggingInterceptor.java} (72%) create mode 100644 src/main/resources/logback-spring.xml diff --git a/build.gradle b/build.gradle index 20ac04d..6d2f809 100644 --- a/build.gradle +++ b/build.gradle @@ -38,6 +38,8 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-validation' implementation group: 'commons-codec', name: 'commons-codec', version: '1.9' implementation 'com.google.code.gson:gson:2.10.1' + implementation 'com.google.guava:guava:11.0.2' + implementation group: 'net.jodah', name: 'typetools', version: '0.4.2' developmentOnly 'org.springframework.boot:spring-boot-devtools' diff --git a/src/main/java/com/dnd/gooding/springconfig/web/MdcLoggingInterceptor.java b/src/main/java/com/dnd/gooding/springconfig/web/MDCLoggingInterceptor.java similarity index 72% rename from src/main/java/com/dnd/gooding/springconfig/web/MdcLoggingInterceptor.java rename to src/main/java/com/dnd/gooding/springconfig/web/MDCLoggingInterceptor.java index 042e060..8c94d1e 100644 --- a/src/main/java/com/dnd/gooding/springconfig/web/MdcLoggingInterceptor.java +++ b/src/main/java/com/dnd/gooding/springconfig/web/MDCLoggingInterceptor.java @@ -8,9 +8,12 @@ import org.springframework.web.servlet.HandlerInterceptor; @Slf4j -public class MdcLoggingInterceptor implements HandlerInterceptor { +public class MDCLoggingInterceptor implements HandlerInterceptor { public static final String REQUEST_CONTROLLER_MDC_KEY = "handler"; + private static final String REQUEST_URL = "requestUrl"; + private static final String REMOTE_ADDR = "remoteAddr"; + private static final String REQUEST_METHOD = "requestMethod"; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) @@ -20,6 +23,9 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons String methodName = handlerMethod.getMethod().getName(); String controllerInfo = handlerName + "." + methodName; MDC.put(REQUEST_CONTROLLER_MDC_KEY, controllerInfo); + MDC.put(REMOTE_ADDR, request.getRemoteAddr()); + MDC.put(REQUEST_METHOD, request.getMethod()); + MDC.put(REQUEST_URL, String.valueOf(request.getRequestURL())); } return true; } diff --git a/src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java b/src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java index a225a1b..722b43c 100644 --- a/src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java +++ b/src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java @@ -9,6 +9,6 @@ public class WebMvcConfig implements WebMvcConfigurer { @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(new MdcLoggingInterceptor()); + registry.addInterceptor(new MDCLoggingInterceptor()); } } diff --git a/src/main/resources/logback-spring.xml b/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..3966427 --- /dev/null +++ b/src/main/resources/logback-spring.xml @@ -0,0 +1,54 @@ + + + + + + + + + + + + + + + + + ${LOG_PATTERN} + + + + + + + ${LOG_PATH}/${LOG_FILE_NAME}.log + + + ${LOG_PATTERN} + + + + + ${LOG_PATH}/%d{yyyy-MM, aux}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}.log + + + + 20GB + + + + + + + + + + + \ No newline at end of file