-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
110 additions
and
0 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
40 changes: 40 additions & 0 deletions
40
src/main/java/com/dnd/gooding/springconfig/web/MDCLoggingInterceptor.java
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,40 @@ | ||
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"; | ||
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) | ||
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); | ||
MDC.put(REMOTE_ADDR, request.getRemoteAddr()); | ||
MDC.put(REQUEST_METHOD, request.getMethod()); | ||
MDC.put(REQUEST_URL, String.valueOf(request.getRequestURL())); | ||
} | ||
return true; | ||
} | ||
|
||
/** MDC에 저장된 정보를 모두 초기화 한다. */ | ||
@Override | ||
public void afterCompletion( | ||
HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) | ||
throws Exception { | ||
MDC.clear(); | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
src/main/java/com/dnd/gooding/springconfig/web/WebMvcConfig.java
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,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()); | ||
} | ||
} |
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,54 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- 60초마다 설정 파일의 변경을 확인 하여 변경시 갱신 --> | ||
<configuration scan="true" scanPeriod="60 seconds"> | ||
<!-- 로그 파일이 저장될 경로 --> | ||
<property name="LOG_PATH" value="log"/> | ||
<!-- 로그 파일 이름 --> | ||
<property name="LOG_FILE_NAME" value="was"/> | ||
<!-- 로그 출력 패턴 --> | ||
<property name="LOG_PATTERN" value="[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%X{remoteAddr}] [%thread, %X{traceId:-}, %X{spanId:-}] [%logger{40}] [%X{requestMethod} %X{requestUrl} %X{handler}] - %msg%n"/> | ||
<!-- 로그 레벨 --> | ||
<!-- | ||
1) ERROR : 오류 메시지 표시 | ||
2) WARN : 경고성 메시지 표시 | ||
3) INFO : 정보성 메시지 표시 | ||
4) DEBUG : 디버깅하기 위한 메시지 표시 | ||
5) TRACE : Debug보다 훨씬 상세한 메시지 표시 | ||
아래에서는 info로 설정하였는데, 이 경우엔 INFO보다 위에 있는 DEBUG와 TRACE는 표시하지 않는다. | ||
--> | ||
<property name="LOG_LEVEL" value="info"/> | ||
|
||
<!-- CONSOLE에 로그 출력 세팅 --> | ||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
<Pattern>${LOG_PATTERN}</Pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<!-- File에 로그 출력 세팅 --> | ||
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> | ||
<!-- 파일 경로 설정 --> | ||
<file>${LOG_PATH}/${LOG_FILE_NAME}.log</file> | ||
<!-- 출력패턴 설정--> | ||
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> | ||
<pattern>${LOG_PATTERN}</pattern> | ||
</encoder> | ||
<!-- Rolling 정책 --> | ||
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> | ||
<!-- .gz,.zip 등을 넣으면 자동 일자별 로그파일 압축 --> | ||
<fileNamePattern>${LOG_PATH}/%d{yyyy-MM, aux}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}.log</fileNamePattern> | ||
<!-- 일자별 로그파일 최대 보관주기(~일), 해당 설정일 이상된 파일은 자동으로 제거--> | ||
<!-- <maxHistory>30</maxHistory> --> | ||
<!-- 로그 파일 최대 보관 크기. 최대 크기를 초과하면 가장 오래된 로그 자동 제거 --> | ||
<totalSizeCap>20GB</totalSizeCap> | ||
</rollingPolicy> | ||
</appender> | ||
|
||
<!-- 로그 전역 세팅 --> | ||
<root level="${LOG_LEVEL}"> | ||
<!-- 위에 설정한 콘솔 설정 추가 --> | ||
<appender-ref ref="CONSOLE"/> | ||
<!-- 위에 설정한 파일 설정 추가 --> | ||
<appender-ref ref="FILE"/> | ||
</root> | ||
</configuration> |