-
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
13 changed files
with
381 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<included> | ||
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
</included> |
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,18 @@ | ||
<included> | ||
<appender name="DISCORD" class="org.dateroad.feign.discord.DiscordLogAppender"> | ||
<param name="discordLogUrl" value="${discordLogUrl}" /> | ||
</appender> | ||
<appender name="ASYNC_DISCORD" class="ch.qos.logback.classic.AsyncAppender"> | ||
<appender-ref ref="DISCORD" /> | ||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | ||
<level>ERROR</level> | ||
</filter> | ||
</appender> | ||
<!-- 비동기 Discord 로그 설정 --> | ||
<appender name="ASYNC_DISCORD" class="ch.qos.logback.classic.AsyncAppender"> | ||
<appender-ref ref="DISCORD" /> | ||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | ||
<level>ERROR</level> | ||
</filter> | ||
</appender> | ||
</included> |
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,12 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<configuration> | ||
<springProperty name="discordLogUrl" source="feign.discord.webhook.log-url"/> | ||
<include resource="console-appender.xml"/> | ||
<include resource="discord-appender.xml"/> | ||
|
||
<!-- 로그 레벨 지정 --> | ||
<root level="INFO"> | ||
<appender-ref ref="CONSOLE" /> | ||
<appender-ref ref="ASYNC_DISCORD" /> | ||
</root> | ||
</configuration> |
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
3 changes: 2 additions & 1 deletion
3
dateroad-common/src/main/java/org/dateroad/event/SignUpEventInfo.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
29 changes: 29 additions & 0 deletions
29
dateroad-common/src/main/java/org/dateroad/mdc/MDCFilter.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,29 @@ | ||
package org.dateroad.mdc; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
import org.springframework.web.util.WebUtils; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
public class MDCFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
HttpServletRequest httpReq = WebUtils.getNativeRequest(request, HttpServletRequest.class); | ||
|
||
MDCManager.setJsonValue(MDCManager.MDC_REQUEST_URI, MDCManager.getRequestUri(httpReq)); | ||
MDCManager.setJsonValue(MDCManager.MDC_USER_IP, MDCManager.getUserIP(httpReq)); | ||
MDCManager.setJsonValue(MDCManager.MDC_REQUEST_COOKIES, MDCManager.getCookies(httpReq)); | ||
MDCManager.setJsonValue(MDCManager.MDC_REQUEST_ORIGIN, MDCManager.getRequestOrigin(httpReq)); | ||
MDCManager.setJsonValue(MDCManager.MDC_HEADER, MDCManager.getHeader(httpReq)); | ||
MDCManager.setJsonValue(MDCManager.MDC_PARAMETER, MDCManager.getParameter(httpReq)); | ||
MDCManager.set(MDCManager.MDC_BODY, MDCManager.getBody(httpReq)); | ||
|
||
filterChain.doFilter(request, response); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
dateroad-common/src/main/java/org/dateroad/mdc/MDCFilterConfig.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,24 @@ | ||
package org.dateroad.mdc; | ||
|
||
import org.springframework.boot.web.servlet.FilterRegistrationBean; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Profile; | ||
|
||
@Profile("!local") | ||
@Configuration | ||
public class MDCFilterConfig { | ||
@Bean | ||
public FilterRegistrationBean<RequestWrappingFilter> secondFilter() { | ||
FilterRegistrationBean<RequestWrappingFilter> filterRegistrationBean = new FilterRegistrationBean<>(new RequestWrappingFilter()); | ||
filterRegistrationBean.setOrder(0); | ||
return filterRegistrationBean; | ||
} | ||
|
||
@Bean | ||
public FilterRegistrationBean<MDCFilter> thirdFilter() { | ||
FilterRegistrationBean<MDCFilter> filterRegistrationBean = new FilterRegistrationBean<>(new MDCFilter()); | ||
filterRegistrationBean.setOrder(1); | ||
return filterRegistrationBean; | ||
} | ||
} |
97 changes: 97 additions & 0 deletions
97
dateroad-common/src/main/java/org/dateroad/mdc/MDCManager.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,97 @@ | ||
package org.dateroad.mdc; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.servlet.http.Cookie; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.slf4j.MDC; | ||
import org.slf4j.spi.MDCAdapter; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.util.WebUtils; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
@Component | ||
public class MDCManager { | ||
public static final String MDC_REQUEST_URI = "Request URI"; | ||
public static final String MDC_USER_IP = "사용자 IP"; | ||
public static final String MDC_REQUEST_COOKIES = "Request Cookie"; | ||
public static final String MDC_REQUEST_ORIGIN = "Request Origin"; | ||
public static final String MDC_HEADER = "HTTP Header"; | ||
public static final String MDC_PARAMETER = "Parameter"; | ||
public static final String MDC_BODY = "HTTP Body"; | ||
private static final ObjectMapper objectMapper = new ObjectMapper(); | ||
private static final MDCAdapter mdcAdapter = MDC.getMDCAdapter(); | ||
public static void set(String key, String value) { | ||
mdcAdapter.put(key, value); | ||
} | ||
|
||
public static Object get(String key) { | ||
return mdcAdapter.get(key); | ||
} | ||
|
||
public static void setJsonValue(String key, Object value) throws JsonProcessingException { | ||
try { | ||
if (value != null) { | ||
String json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(value); | ||
mdcAdapter.put(key, json); | ||
} else { | ||
mdcAdapter.put(key, "내용이 없습니다."); | ||
} | ||
} catch (JsonProcessingException ex) { | ||
throw ex; | ||
} | ||
} | ||
|
||
public static String getRequestUri(HttpServletRequest request) { | ||
return request.getRequestURI(); | ||
} | ||
|
||
public static String getUserIP(HttpServletRequest request) { | ||
String ip = request.getHeader("X-Forwarded-For"); | ||
if (ip == null) | ||
ip = request.getRemoteAddr(); | ||
|
||
return ip; | ||
} | ||
|
||
public static Cookie[] getCookies(HttpServletRequest request) { | ||
return request.getCookies(); | ||
} | ||
|
||
public static String getRequestOrigin(HttpServletRequest request) { | ||
return request.getHeader("Origin"); | ||
} | ||
|
||
public static Map<String, String> getHeader(HttpServletRequest request) { | ||
Map<String, String> headerMap = new HashMap<>(); | ||
request.getHeaderNames().asIterator() | ||
.forEachRemaining(name -> { | ||
if (!name.equals("user-agent")) { | ||
headerMap.put(name, request.getHeader(name)); | ||
} | ||
}); | ||
return headerMap; | ||
} | ||
|
||
public static Map<String, String> getParameter(HttpServletRequest request) { | ||
Map<String, String> paramMap = new HashMap<>(); | ||
request.getParameterNames().asIterator() | ||
.forEachRemaining(name -> paramMap.put(name, request.getParameter(name))); | ||
|
||
return paramMap; | ||
} | ||
|
||
public static String getBody(HttpServletRequest request) { | ||
RequestBodyWrapper requestBodyWrapper = WebUtils.getNativeRequest(request, RequestBodyWrapper.class); | ||
|
||
if (requestBodyWrapper != null) { | ||
return requestBodyWrapper.getRequestBody(); | ||
} | ||
|
||
return "requestBody 정보 없음"; | ||
} | ||
} |
66 changes: 66 additions & 0 deletions
66
dateroad-common/src/main/java/org/dateroad/mdc/RequestBodyWrapper.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,66 @@ | ||
package org.dateroad.mdc; | ||
|
||
import jakarta.servlet.ReadListener; | ||
import jakarta.servlet.ServletInputStream; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletRequestWrapper; | ||
import lombok.Getter; | ||
import org.dateroad.code.FailureCode; | ||
import org.dateroad.exception.BadRequestException; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
|
||
@Getter | ||
public class RequestBodyWrapper extends HttpServletRequestWrapper { | ||
private final String requestBody; | ||
|
||
public RequestBodyWrapper(HttpServletRequest request) { | ||
super(request); | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
try (BufferedReader bufferedReader = request.getReader()) { | ||
char[] charBuffer = new char[128]; | ||
int bytesRead; | ||
while ((bytesRead = bufferedReader.read(charBuffer)) > 0) { | ||
stringBuilder.append(charBuffer, 0, bytesRead); | ||
} | ||
} catch (IOException e) { | ||
throw new BadRequestException(FailureCode.BAD_REQUEST); | ||
} | ||
|
||
requestBody = stringBuilder.toString(); | ||
} | ||
|
||
@Override | ||
public ServletInputStream getInputStream() { | ||
final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(requestBody.getBytes()); | ||
return new ServletInputStream() { | ||
@Override | ||
public boolean isFinished() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public boolean isReady() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public void setReadListener(ReadListener readListener) { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
public int read() { | ||
return byteArrayInputStream.read(); | ||
} | ||
}; | ||
} | ||
|
||
@Override | ||
public BufferedReader getReader() { | ||
return new BufferedReader(new InputStreamReader(this.getInputStream())); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
dateroad-common/src/main/java/org/dateroad/mdc/RequestWrappingFilter.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,19 @@ | ||
package org.dateroad.mdc; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
@Slf4j | ||
public class RequestWrappingFilter extends OncePerRequestFilter { | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
RequestBodyWrapper requestBodyWrapper = new RequestBodyWrapper(request); | ||
filterChain.doFilter(requestBodyWrapper, response); | ||
} | ||
} |
Oops, something went wrong.