Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[#35] Refactor : 로그 설정 변경 #45

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .docker/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ services:
- db
ports:
- "8080:8080"
volumes:
- ./.log:/.log
environment:
DB_USER_NAME: ${DB_USER_NAME}
DB_PASSWORD: ${DB_PASSWORD}
Expand Down Expand Up @@ -66,6 +68,8 @@ services:
- db
ports:
- "8081:8080"
volumes:
- ./.log:/.log
environment:
DB_USER_NAME: ${DB_USER_NAME}
DB_PASSWORD: ${DB_PASSWORD}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.clover.habbittracker.global.infra.log;

import static com.clover.habbittracker.global.infra.log.MyLogger.ClientIPHeader.*;
import static org.springframework.web.servlet.HandlerMapping.*;

import org.springframework.web.method.HandlerMethod;
Expand All @@ -15,7 +16,7 @@ public class MyLogger {

private static final String LOG_FORM = "[{}]: Called By method [{}] Cause : {} id = {}";

private static final String COMMON_LOG_FORM = "[{}]: Called By method {} Cause : {}";
private static final String COMMON_LOG_FORM = "[{}]: Called By method {} Cause : {} Reason : {} client IP : {}";

public static void warnExceptionLogging(HttpServletRequest request, BaseException e) {
ErrorType errorType = e.getErrorType();
Expand All @@ -28,15 +29,42 @@ public static void warnExceptionLogging(HttpServletRequest request, BaseExceptio
public static void commonExceptionWarnLogging(HttpServletRequest request, Exception e, ErrorType errorType) {
String methodName
= ((HandlerMethod)request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).getMethod().getName();

log.warn(COMMON_LOG_FORM, e.getClass().getSimpleName(), methodName, errorType.getErrorMsg());
String clientIP = getClientIP(request);
log.warn(COMMON_LOG_FORM, e.getClass().getSimpleName(), methodName, errorType.getErrorMsg(), e.getMessage(),
clientIP);
}

public static void commonExceptionErrorLogging(HttpServletRequest request, Exception e, ErrorType errorType) {
String methodName
= ((HandlerMethod)request.getAttribute(BEST_MATCHING_HANDLER_ATTRIBUTE)).getMethod().getName();

log.warn(COMMON_LOG_FORM, e.getClass().getSimpleName(), methodName, errorType.getErrorMsg());
String clientIP = getClientIP(request);
log.warn(COMMON_LOG_FORM, e.getClass().getSimpleName(), methodName, errorType.getErrorMsg(), e.getMessage(),
clientIP);
}

public enum ClientIPHeader {
X_FORWARDED_FOR("X-Forwarded-For"),
PROXY_CLIENT_IP("Proxy-Client-IP"),
WL_PROXY_CLIENT_IP("WL-Proxy-Client-IP"),
HTTP_CLIENT_IP("HTTP_CLIENT_IP"),
HTTP_X_FORWARDED_FOR("HTTP_X_FORWARDED_FOR"),
REMOTE_ADDR(null);

private final String headerName;

ClientIPHeader(String headerName) {
this.headerName = headerName;
}

public static String getClientIP(HttpServletRequest request) {
String ip = null;
for (ClientIPHeader header : values()) {
ip = request.getHeader(header.headerName);
}
if (ip == null) {
ip = request.getRemoteAddr();
}
return ip;
}
}
}
29 changes: 29 additions & 0 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,33 @@
<!-- 로그 파일 최대 보관 크기. 최대 크기를 초과하면 가장 오래된 로그 자동 제거 -->
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>DENY</onMatch>
</filter>
</appender>

<appender name="ERROR_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- 파일 경로 설정 -->
<file>${LOG_PATH}/${LOG_FILE_NAME}_error.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-dd}/${LOG_FILE_NAME}.%d{yyyy-MM-dd}_error.log</fileNamePattern>
<!-- 일자별 로그파일 최대 보관주기(~일), 해당 설정일 이상된 파일은 자동으로 제거-->
<!-- <maxHistory>30</maxHistory> -->
<!-- 로그 파일 최대 보관 크기. 최대 크기를 초과하면 가장 오래된 로그 자동 제거 -->
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
</appender>

<!-- 로그 전역 세팅 -->
Expand All @@ -50,5 +77,7 @@
<appender-ref ref="CONSOLE"/>
<!-- 위에 설정한 파일 설정 추가 -->
<appender-ref ref="FILE"/>

<appender-ref ref="ERROR_FILE"/>
</root>
</configuration>