From 006559de06dfe3b7f721ab935dbac2e8d78768fb Mon Sep 17 00:00:00 2001 From: sohyundoh Date: Tue, 29 Oct 2024 22:40:29 +0900 Subject: [PATCH] =?UTF-8?q?#550=20[feat]=20DISCORD=20=EC=A0=9C=EA=B1=B0=20?= =?UTF-8?q?=EB=B0=8F=20request=20Id=20=EB=A1=9C=EA=B9=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../mile/common/filter/MDCLoggingFilter.java | 24 +++ .../log/filter => config}/FilterConfig.java | 10 +- .../log/discord/DiscordAppender.java | 114 ------------- .../exception/log/discord/DiscordWebHook.java | 150 ------------------ .../exception/ErrorLogAppenderException.java | 10 -- .../exception/log/discord/model/Author.java | 13 -- .../log/discord/model/EmbedObject.java | 133 ---------------- .../exception/log/discord/model/Field.java | 13 -- .../exception/log/discord/model/Footer.java | 12 -- .../exception/log/discord/model/Image.java | 12 -- .../log/discord/model/JsonObject.java | 56 ------- .../log/discord/model/Thumbnail.java | 11 -- .../mile/exception/log/filter/MDCFilter.java | 40 ----- .../mile/exception/log/utils/ApiCallUtil.java | 37 ----- .../log/utils/HttpRequestConfig.java | 19 --- .../exception/log/utils/HttpRequestUtil.java | 89 ----------- .../com/mile/exception/log/utils/MDCUtil.java | 37 ----- .../mile/exception/log/utils/StringUtil.java | 85 ---------- .../src/main/resources/discord-appender.xml | 14 -- .../src/main/resources/logback-spring.xml | 7 +- module-domain/build.gradle | 3 + 21 files changed, 34 insertions(+), 855 deletions(-) create mode 100644 module-api/src/main/java/com/mile/common/filter/MDCLoggingFilter.java rename module-api/src/main/java/com/mile/{exception/log/filter => config}/FilterConfig.java (65%) delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/DiscordAppender.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/DiscordWebHook.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/exception/ErrorLogAppenderException.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/model/Author.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/model/EmbedObject.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/model/Field.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/model/Footer.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/model/Image.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/model/JsonObject.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/discord/model/Thumbnail.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/filter/MDCFilter.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/utils/ApiCallUtil.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/utils/HttpRequestConfig.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/utils/HttpRequestUtil.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/utils/MDCUtil.java delete mode 100644 module-api/src/main/java/com/mile/exception/log/utils/StringUtil.java delete mode 100644 module-api/src/main/resources/discord-appender.xml diff --git a/module-api/src/main/java/com/mile/common/filter/MDCLoggingFilter.java b/module-api/src/main/java/com/mile/common/filter/MDCLoggingFilter.java new file mode 100644 index 00000000..0e4cdbb9 --- /dev/null +++ b/module-api/src/main/java/com/mile/common/filter/MDCLoggingFilter.java @@ -0,0 +1,24 @@ +package com.mile.common.filter; + + +import jakarta.servlet.Filter; +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.ServletRequest; +import jakarta.servlet.ServletResponse; +import org.slf4j.MDC; +import org.springframework.stereotype.Component; + +import java.io.IOException; +import java.util.UUID; + +@Component +public class MDCLoggingFilter implements Filter { + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { + final String requestId = UUID.randomUUID().toString(); + MDC.put("request_id", requestId); + chain.doFilter(request, response); + MDC.clear(); + } +} diff --git a/module-api/src/main/java/com/mile/exception/log/filter/FilterConfig.java b/module-api/src/main/java/com/mile/config/FilterConfig.java similarity index 65% rename from module-api/src/main/java/com/mile/exception/log/filter/FilterConfig.java rename to module-api/src/main/java/com/mile/config/FilterConfig.java index 00880e39..2bf06557 100644 --- a/module-api/src/main/java/com/mile/exception/log/filter/FilterConfig.java +++ b/module-api/src/main/java/com/mile/config/FilterConfig.java @@ -1,6 +1,8 @@ -package com.mile.exception.log.filter; +package com.mile.config; +import com.mile.common.filter.MDCLoggingFilter; +import com.mile.exception.log.filter.CustomServletWrappingFilter; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @@ -16,9 +18,9 @@ public FilterRegistrationBean secondFilter() { } @Bean - public FilterRegistrationBean thirdFilter() { - FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>( - new MDCFilter()); + public FilterRegistrationBean thirdFilter() { + FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean<>( + new MDCLoggingFilter()); filterRegistrationBean.setOrder(1); return filterRegistrationBean; } diff --git a/module-api/src/main/java/com/mile/exception/log/discord/DiscordAppender.java b/module-api/src/main/java/com/mile/exception/log/discord/DiscordAppender.java deleted file mode 100644 index bcb0483e..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/DiscordAppender.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.mile.exception.log.discord; - -import ch.qos.logback.classic.spi.ILoggingEvent; -import ch.qos.logback.classic.spi.IThrowableProxy; -import ch.qos.logback.classic.spi.ThrowableProxyUtil; -import ch.qos.logback.core.UnsynchronizedAppenderBase; -import com.mile.exception.log.discord.exception.ErrorLogAppenderException; -import com.mile.exception.log.discord.model.EmbedObject; -import com.mile.exception.log.utils.MDCUtil; -import com.mile.exception.log.utils.StringUtil; -import lombok.Setter; -import lombok.extern.slf4j.Slf4j; -import org.apache.commons.lang3.StringEscapeUtils; - -import java.awt.*; -import java.io.IOException; -import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; -import java.util.Map; - -@Slf4j -@Setter -public class DiscordAppender extends UnsynchronizedAppenderBase { - - private String discordWebhookUrl; - private String username; - private String avatarUrl; - - private static Color getLevelColor(ILoggingEvent eventObject) { - String level = eventObject.getLevel().levelStr; - if (level.equals("WARN")) { - return Color.yellow; - } else if (level.equals("ERROR")) { - return Color.red; - } - - return Color.blue; - } - - @Override - protected void append(ILoggingEvent eventObject) { - DiscordWebHook discordWebhook = new DiscordWebHook(discordWebhookUrl, username, avatarUrl, false); - Map mdcPropertyMap = eventObject.getMDCPropertyMap(); - Color messageColor = getLevelColor(eventObject); - - String level = eventObject.getLevel().levelStr; - String exceptionBrief = ""; - String exceptionDetail = ""; - IThrowableProxy throwable = eventObject.getThrowableProxy(); - log.info("{}", eventObject.getMessage()); - - if (throwable != null) { - exceptionBrief = throwable.getClassName() + ": " + throwable.getMessage(); - } - - if (exceptionBrief.equals("")) { - exceptionBrief = "EXCEPTION 정보가 남지 않았습니다."; - } - - discordWebhook.addEmbed(new EmbedObject() - .setTitle("[" + level + " - 문제 간략 내용]") - .setColor(messageColor) - .setDescription(exceptionBrief) - .addField("[" + "Exception Level" + "]", - StringEscapeUtils.escapeJson(level), - true) - .addField("[문제 발생 시각]", - LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), - false) - .addField( - "[" + MDCUtil.REQUEST_URI_MDC + "]", - StringEscapeUtils.escapeJson(mdcPropertyMap.get(MDCUtil.REQUEST_URI_MDC)), - false) - .addField( - "[" + MDCUtil.USER_IP_MDC + "]", - StringEscapeUtils.escapeJson(mdcPropertyMap.get(MDCUtil.USER_IP_MDC)), - false) - .addField( - "[" + MDCUtil.HEADER_MAP_MDC + "]", - StringEscapeUtils.escapeJson(mdcPropertyMap.get(MDCUtil.HEADER_MAP_MDC).replaceAll("[\\{\\{\\}]", "")), - true) - .addField( - "[" + MDCUtil.USER_REQUEST_COOKIES + "]", - StringEscapeUtils.escapeJson( - mdcPropertyMap.get(MDCUtil.USER_REQUEST_COOKIES).replaceAll("[\\{\\{\\}]", "")), - false) - .addField( - "[" + MDCUtil.PARAMETER_MAP_MDC + "]", - StringEscapeUtils.escapeJson( - mdcPropertyMap.get(MDCUtil.PARAMETER_MAP_MDC).replaceAll("[\\{\\{\\}]", "")), - false) - .addField("[" + MDCUtil.BODY_MDC + "]", - StringEscapeUtils.escapeJson(StringUtil.translateEscapes(mdcPropertyMap.get(MDCUtil.BODY_MDC))), - false) - ); - - if (throwable != null) { - exceptionDetail = ThrowableProxyUtil.asString(throwable); - String exception = exceptionDetail.substring(0, 4000); - discordWebhook.addEmbed( - new EmbedObject() - .setTitle("[Exception 상세 내용]") - .setColor(messageColor) - .setDescription(StringEscapeUtils.escapeJson(exception)) - ); - } - - try { - discordWebhook.execute(); - } catch (IOException ioException) { - throw new ErrorLogAppenderException(); - } - } -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/discord/DiscordWebHook.java b/module-api/src/main/java/com/mile/exception/log/discord/DiscordWebHook.java deleted file mode 100644 index 1eaefa28..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/DiscordWebHook.java +++ /dev/null @@ -1,150 +0,0 @@ -package com.mile.exception.log.discord; - - -import com.mile.exception.log.discord.exception.ErrorLogAppenderException; -import com.mile.exception.log.discord.model.Author; -import com.mile.exception.log.discord.model.EmbedObject; -import com.mile.exception.log.discord.model.Field; -import com.mile.exception.log.discord.model.Footer; -import com.mile.exception.log.discord.model.JsonObject; -import com.mile.exception.log.discord.model.Thumbnail; -import com.mile.exception.log.utils.ApiCallUtil; -import com.mile.exception.log.discord.model.Image; - -import java.awt.Color; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - -public class DiscordWebHook { - - private final String urlString; - private final List embeds = new ArrayList<>(); - private String username; - private String avatarUrl; - private boolean tts; - - public DiscordWebHook(String urlString, String username, String avatarUrl, boolean tts) { - this.urlString = urlString; - this.username = username; - this.avatarUrl = avatarUrl; - this.tts = tts; - } - - public void addEmbed(EmbedObject embed) { - this.embeds.add(embed); - } - - public void execute() throws IOException { - if (this.embeds.isEmpty()) { - throw new RuntimeException("컨텐츠를 설정하거나 하나 이상의 Embed Object를 추가해야 합니다."); - } - - try { - ApiCallUtil.callDiscordAppenderPostAPI( - this.urlString, createDiscordEmbedObject( - this.embeds, initializerDiscordSendForJsonObject(new JsonObject()) - )); - - } catch (IOException ioException) { - throw ioException; - } - } - - private JsonObject initializerDiscordSendForJsonObject(JsonObject json) { - json.put("username", this.username); - json.put("avatar_url", this.avatarUrl); - json.put("tts", this.tts); - return json; - } - - private JsonObject createDiscordEmbedObject(List embeds, JsonObject json) { - if (embeds.isEmpty()) { - throw new ErrorLogAppenderException(); - } - - List embedObjects = new ArrayList<>(); - - for (EmbedObject embed : embeds) { - JsonObject jsonEmbed = new JsonObject(); - - jsonEmbed.put("title", embed.getTitle()); - jsonEmbed.put("description", embed.getDescription()); - jsonEmbed.put("url", embed.getUrl()); - - processDiscordEmbedColor(embed, jsonEmbed); - processDiscordEmbedFooter(embed.getFooter(), jsonEmbed); - processDiscordEmbedImage(embed.getImage(), jsonEmbed); - processDiscordEmbedThumbnail(embed.getThumbnail(), jsonEmbed); - processDiscordEmbedAuthor(embed.getAuthor(), jsonEmbed); - processDiscordEmbedMessageFields(embed.getFields(), jsonEmbed); - - embedObjects.add(jsonEmbed); - } - json.put("embeds", embedObjects.toArray()); - - return json; - } - - private void processDiscordEmbedColor(EmbedObject embed, JsonObject jsonEmbed) { - if (embed.getColor() != null) { - Color color = embed.getColor(); - int rgb = color.getRed(); - rgb = (rgb << 8) + color.getGreen(); - rgb = (rgb << 8) + color.getBlue(); - - jsonEmbed.put("color", rgb); - } - } - - private void processDiscordEmbedFooter(Footer footer, JsonObject jsonEmbed) { - if (footer != null) { - JsonObject jsonFooter = new JsonObject(); - jsonFooter.put("text", footer.getText()); - jsonFooter.put("icon_url", footer.getIconUrl()); - jsonEmbed.put("footer", jsonFooter); - } - } - - private void processDiscordEmbedImage(Image image, JsonObject jsonEmbed) { - if (image != null) { - JsonObject jsonImage = new JsonObject(); - jsonImage.put("url", image.getUrl()); - jsonEmbed.put("image", jsonImage); - } - } - - private void processDiscordEmbedThumbnail(Thumbnail thumbnail, JsonObject jsonEmbed) { - if (thumbnail != null) { - JsonObject jsonThumbnail = new JsonObject(); - jsonThumbnail.put("url", thumbnail.getUrl()); - jsonEmbed.put("thumbnail", jsonThumbnail); - } - } - - private void processDiscordEmbedAuthor(Author author, JsonObject jsonEmbed) { - if (author != null) { - JsonObject jsonAuthor = new JsonObject(); - jsonAuthor.put("name", author.getName()); - jsonAuthor.put("url", author.getUrl()); - jsonAuthor.put("icon_url", author.getIconUrl()); - jsonEmbed.put("author", jsonAuthor); - } - } - - private void processDiscordEmbedMessageFields(List fields, JsonObject jsonEmbed) { - List jsonFields = new ArrayList<>(); - - for (Field field : fields) { - JsonObject jsonField = new JsonObject(); - - jsonField.put("name", field.getName()); - jsonField.put("value", field.getValue()); - jsonField.put("inline", field.isInline()); - - jsonFields.add(jsonField); - } - - jsonEmbed.put("fields", jsonFields.toArray()); - } -} diff --git a/module-api/src/main/java/com/mile/exception/log/discord/exception/ErrorLogAppenderException.java b/module-api/src/main/java/com/mile/exception/log/discord/exception/ErrorLogAppenderException.java deleted file mode 100644 index 554385a2..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/exception/ErrorLogAppenderException.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.mile.exception.log.discord.exception; - -import com.mile.exception.message.ErrorMessage; -import com.mile.exception.model.MileException; - -public class ErrorLogAppenderException extends MileException { - public ErrorLogAppenderException() { - super(ErrorMessage.DISCORD_LOG_APPENDER_ERROR); - } -} diff --git a/module-api/src/main/java/com/mile/exception/log/discord/model/Author.java b/module-api/src/main/java/com/mile/exception/log/discord/model/Author.java deleted file mode 100644 index 3dc699f8..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/model/Author.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mile.exception.log.discord.model; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@Getter -@RequiredArgsConstructor -public class Author { - - private final String name; - private final String url; - private final String iconUrl; -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/discord/model/EmbedObject.java b/module-api/src/main/java/com/mile/exception/log/discord/model/EmbedObject.java deleted file mode 100644 index 5ad41480..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/model/EmbedObject.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.mile.exception.log.discord.model; - -import java.awt.Color; -import java.util.ArrayList; -import java.util.List; - -public class EmbedObject { - /** - * Discord Embed Message에 들어갈 Message 내용 List - */ - private final List fields = new ArrayList<>(); - - /** - * Discord Embed Message 제목 - */ - private String title; - - /** - * Discord Embed Message 상세 내용 - */ - private String description; - - /** - * Discord Embed Message URL 형식 - */ - private String url; - - /** - * Discord Embed Message 겉 색깔 - */ - private Color color; - - /** - * Discord Embed Message 바닥글 - */ - private Footer footer; - - /** - * Discord Embed Message 썸네일 - */ - private Thumbnail thumbnail; - - /** - * Discord Embed Message 사진 - */ - private Image image; - - /** - * Discord Embed Message 작성자 - */ - private Author author; - - public String getTitle() { - return title; - } - - public EmbedObject setTitle(String title) { - this.title = title; - return this; - } - - public String getDescription() { - return description; - } - - public EmbedObject setDescription(String description) { - this.description = description; - return this; - } - - public String getUrl() { - return url; - } - - public EmbedObject setUrl(String url) { - this.url = url; - return this; - } - - public Color getColor() { - return color; - } - - public EmbedObject setColor(Color color) { - this.color = color; - return this; - } - - public Footer getFooter() { - return footer; - } - - public Thumbnail getThumbnail() { - return thumbnail; - } - - public EmbedObject setThumbnail(String url) { - this.thumbnail = new Thumbnail(url); - return this; - } - - public Image getImage() { - return image; - } - - public EmbedObject setImage(String url) { - this.image = new Image(url); - return this; - } - - public Author getAuthor() { - return author; - } - - public List getFields() { - return fields; - } - - public EmbedObject setFooter(String text, String icon) { - this.footer = new Footer(text, icon); - return this; - } - - public EmbedObject setAuthor(String name, String url, String icon) { - this.author = new Author(name, url, icon); - return this; - } - - public EmbedObject addField(String name, String value, boolean inline) { - this.fields.add(new Field(name, value, inline)); - return this; - } -} diff --git a/module-api/src/main/java/com/mile/exception/log/discord/model/Field.java b/module-api/src/main/java/com/mile/exception/log/discord/model/Field.java deleted file mode 100644 index 5e8cb355..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/model/Field.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.mile.exception.log.discord.model; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@Getter -@RequiredArgsConstructor -public class Field { - - private final String name; - private final String value; - private final boolean inline; -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/discord/model/Footer.java b/module-api/src/main/java/com/mile/exception/log/discord/model/Footer.java deleted file mode 100644 index 5bc38e67..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/model/Footer.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mile.exception.log.discord.model; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@Getter -@RequiredArgsConstructor -public class Footer { - - private final String text; - private final String iconUrl; -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/discord/model/Image.java b/module-api/src/main/java/com/mile/exception/log/discord/model/Image.java deleted file mode 100644 index 9ee3f8c1..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/model/Image.java +++ /dev/null @@ -1,12 +0,0 @@ -package com.mile.exception.log.discord.model; - - -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@Getter -@RequiredArgsConstructor -public class Image { - - private final String url; -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/discord/model/JsonObject.java b/module-api/src/main/java/com/mile/exception/log/discord/model/JsonObject.java deleted file mode 100644 index 18257fc4..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/model/JsonObject.java +++ /dev/null @@ -1,56 +0,0 @@ -package com.mile.exception.log.discord.model; - -import java.lang.reflect.Array; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; -import java.util.Set; - -public class JsonObject { - - private final HashMap discordMessageMap = new HashMap<>(); - - public void put(String key, Object value) { - if (value != null) { - discordMessageMap.put(key, value); - } - } - - @Override - public String toString() { - StringBuilder builder = new StringBuilder(); - Set> entrySet = discordMessageMap.entrySet(); - builder.append("{"); - - int iter = 0; - for (Map.Entry entry : entrySet) { - Object val = entry.getValue(); - builder.append(quote(entry.getKey())).append(":"); - - if (val instanceof String) { - builder.append(quote(String.valueOf(val))); - } else if (val instanceof Integer) { - builder.append(Integer.valueOf(String.valueOf(val))); - } else if (val instanceof Boolean) { - builder.append(val); - } else if (val instanceof JsonObject) { - builder.append(val); - } else if (val.getClass().isArray()) { - builder.append("["); - int len = Array.getLength(val); - for (int j = 0; j < len; j++) { - builder.append(Array.get(val, j).toString()).append(j != len - 1 ? "," : ""); - } - builder.append("]"); - } - - builder.append(++iter == entrySet.size() ? "}" : ","); - } - - return builder.toString(); - } - - private String quote(String string) { - return "\"" + string + "\""; - } -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/discord/model/Thumbnail.java b/module-api/src/main/java/com/mile/exception/log/discord/model/Thumbnail.java deleted file mode 100644 index ed527e97..00000000 --- a/module-api/src/main/java/com/mile/exception/log/discord/model/Thumbnail.java +++ /dev/null @@ -1,11 +0,0 @@ -package com.mile.exception.log.discord.model; - -import lombok.Getter; -import lombok.RequiredArgsConstructor; - -@Getter -@RequiredArgsConstructor -public class Thumbnail { - - private final String url; -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/filter/MDCFilter.java b/module-api/src/main/java/com/mile/exception/log/filter/MDCFilter.java deleted file mode 100644 index 3f741f16..00000000 --- a/module-api/src/main/java/com/mile/exception/log/filter/MDCFilter.java +++ /dev/null @@ -1,40 +0,0 @@ -package com.mile.exception.log.filter; - -import com.mile.exception.log.utils.HttpRequestUtil; -import com.mile.exception.log.utils.MDCUtil; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Component; -import org.springframework.web.filter.OncePerRequestFilter; -import org.springframework.web.util.WebUtils; - -import java.io.IOException; -import java.util.Objects; - -@RequiredArgsConstructor -@Slf4j -@Component -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); - - MDCUtil.setJsonValue(MDCUtil.REQUEST_URI_MDC, HttpRequestUtil.getRequestUri(Objects.requireNonNull(httpReq))); - MDCUtil.setJsonValue(MDCUtil.USER_IP_MDC, HttpRequestUtil.getUserIP(Objects.requireNonNull(httpReq))); - MDCUtil.setJsonValue(MDCUtil.HEADER_MAP_MDC, HttpRequestUtil.getHeaderMap(httpReq)); - MDCUtil.setJsonValue(MDCUtil.USER_REQUEST_COOKIES, HttpRequestUtil.getUserCookies(httpReq)); - MDCUtil.setJsonValue(MDCUtil.PARAMETER_MAP_MDC, HttpRequestUtil.getParamMap(httpReq)); - MDCUtil.setJsonValue(MDCUtil.BODY_MDC, HttpRequestUtil.getBody(httpReq)); - - filterChain.doFilter(request, response); - - } -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/utils/ApiCallUtil.java b/module-api/src/main/java/com/mile/exception/log/utils/ApiCallUtil.java deleted file mode 100644 index 3dbbf00d..00000000 --- a/module-api/src/main/java/com/mile/exception/log/utils/ApiCallUtil.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.mile.exception.log.utils; - - -import java.io.IOException; -import java.io.OutputStream; -import java.net.URL; -import javax.net.ssl.HttpsURLConnection; - -import com.mile.exception.log.discord.model.JsonObject; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; - -@Slf4j -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class ApiCallUtil { - - public static void callDiscordAppenderPostAPI(String urlString, JsonObject json) throws IOException { - URL url = new URL(urlString); - HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(); - connection.addRequestProperty("Content-Type", "application/json"); - connection.addRequestProperty("User-Agent", "Java-DiscordWebhook-BY-Gelox_"); - connection.setDoOutput(true); - connection.setRequestMethod("POST"); - - try (OutputStream stream = connection.getOutputStream()) { - stream.write(json.toString().getBytes()); - stream.flush(); - - connection.getInputStream().close(); - connection.disconnect(); - - } catch (IOException ioException) { - throw ioException; - } - } -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/utils/HttpRequestConfig.java b/module-api/src/main/java/com/mile/exception/log/utils/HttpRequestConfig.java deleted file mode 100644 index 085da5bd..00000000 --- a/module-api/src/main/java/com/mile/exception/log/utils/HttpRequestConfig.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.mile.exception.log.utils; - -import com.mile.exception.log.filter.CustomServletWrappingFilter; -import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -import java.util.Arrays; - -@Configuration -public class HttpRequestConfig { - @Bean - public FilterRegistrationBean reReadableRequestFilter() { - FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new CustomServletWrappingFilter()); - - filterRegistrationBean.setUrlPatterns(Arrays.asList("/*")); - return filterRegistrationBean; - } -} \ No newline at end of file diff --git a/module-api/src/main/java/com/mile/exception/log/utils/HttpRequestUtil.java b/module-api/src/main/java/com/mile/exception/log/utils/HttpRequestUtil.java deleted file mode 100644 index 06d5ca8b..00000000 --- a/module-api/src/main/java/com/mile/exception/log/utils/HttpRequestUtil.java +++ /dev/null @@ -1,89 +0,0 @@ -package com.mile.exception.log.utils; - - -import com.mile.exception.log.filter.wrapper.CachedBodyRequestWrapper; -import jakarta.servlet.http.Cookie; -import jakarta.servlet.http.HttpServletRequest; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.web.client.RestTemplate; -import org.springframework.web.util.WebUtils; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -@Slf4j -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class HttpRequestUtil { - - public static String getRequestUri(HttpServletRequest request) { - return request.getRequestURI(); - } - - public static Map getHeaderMap(HttpServletRequest request) { - Map headerMap = new HashMap<>(); - request.getHeaderNames().asIterator() - .forEachRemaining(name -> { - if (!name.equals("user-agent")) { - headerMap.put(name, request.getHeader(name)); - } - }); - return headerMap; - } - - public static Map getParamMap(HttpServletRequest request) { - Map paramMap = new HashMap<>(); - request.getParameterNames().asIterator() - .forEachRemaining(name -> paramMap.put(name, request.getParameter(name))); - - return paramMap; - } - - public static String getBody(HttpServletRequest httpReq) { - CachedBodyRequestWrapper nativeRequest = WebUtils.getNativeRequest(httpReq, CachedBodyRequestWrapper.class); - - if (nativeRequest != null) { - return nativeRequest.getBody(); - } - return "requestBody 정보 없음"; - } - - public static String getUserIP(HttpServletRequest httpReq) { - String ip = httpReq.getHeader("X-Forwarded-For"); - if (ip == null) - ip = httpReq.getRemoteAddr(); - - return ip; - } - - public static Map getUserLocation(HttpServletRequest request) { - Map locationMap = new HashMap<>(); - String userIP = getUserIP(request); - - String locationFindAPIUrl = "https://ipapi.co/" + userIP + "/json/"; - - RestTemplate restTemplate = new RestTemplate(); - String response = restTemplate.getForObject(locationFindAPIUrl, String.class); - - String[] locationEntity = Objects.requireNonNull(response).split(","); - - for (String entity : locationEntity) { - String[] element = entity.split(":"); - if (element.length == 2) { - locationMap.put( - element[0].replace(" ", "").replace("\n", "").replace("{", "").replace("}", "").replace("\"", ""), - element[1].replace(" ", "").replace("\"", "")); - } else { - locationMap.put("languages", entity); - } - } - - return locationMap; - } - - public static Cookie[] getUserCookies(HttpServletRequest httpReq) { - return httpReq.getCookies(); - } -} diff --git a/module-api/src/main/java/com/mile/exception/log/utils/MDCUtil.java b/module-api/src/main/java/com/mile/exception/log/utils/MDCUtil.java deleted file mode 100644 index c1ab2d0b..00000000 --- a/module-api/src/main/java/com/mile/exception/log/utils/MDCUtil.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.mile.exception.log.utils; - - -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.databind.ObjectMapper; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.slf4j.MDC; -import org.slf4j.spi.MDCAdapter; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class MDCUtil { - - public static final String REQUEST_URI_MDC = "이용자 요청 URI 정보"; - public static final String USER_IP_MDC = "이용자 IP 정보"; - public static final String USER_REQUEST_COOKIES = "이용자 Cookie 정보"; - public static final String HEADER_MAP_MDC = "HTTP 헤더 정보"; - public static final String PARAMETER_MAP_MDC = "Parameter 정보"; - public static final String BODY_MDC = "HTTP Body 정보"; - private static final ObjectMapper objectMapper = new ObjectMapper(); - private static final MDCAdapter mdc = MDC.getMDCAdapter(); - - public static void setJsonValue(String key, Object value) throws JsonProcessingException { - try { - if (value != null) { - String json = objectMapper.writerWithDefaultPrettyPrinter() - .writeValueAsString(value); - mdc.put(key, json); - } else { - mdc.put(key, "내용 없음"); - } - } catch (JsonProcessingException ex) { - throw ex; - } - } - -} diff --git a/module-api/src/main/java/com/mile/exception/log/utils/StringUtil.java b/module-api/src/main/java/com/mile/exception/log/utils/StringUtil.java deleted file mode 100644 index e6741fc6..00000000 --- a/module-api/src/main/java/com/mile/exception/log/utils/StringUtil.java +++ /dev/null @@ -1,85 +0,0 @@ -package com.mile.exception.log.utils; - -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public class StringUtil { - - public static String translateEscapes(String str) { - if (str.isEmpty()) { - return ""; - } - char[] chars = str.toCharArray(); - int length = chars.length; - int from = 0; - int to = 0; - while (from < length) { - char ch = chars[from++]; - if (ch == '\\') { - ch = from < length ? chars[from++] : '\0'; - switch (ch) { - case 'b': - ch = '\b'; - break; - case 'f': - ch = '\f'; - break; - case 'n': - ch = '\n'; - break; - case 'r': - ch = '\r'; - break; - case 's': - ch = ' '; - break; - case 't': - ch = '\t'; - break; - case '\'': - case '\"': - case '\\': - // as is - break; - case '0': - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - int limit = Integer.min(from + (ch <= '3' ? 2 : 1), length); - int code = ch - '0'; - while (from < limit) { - ch = chars[from]; - if (ch < '0' || '7' < ch) { - break; - } - from++; - code = (code << 3) | (ch - '0'); - } - ch = (char) code; - break; - case '\n': - continue; - case '\r': - if (from < length && chars[from] == '\n') { - from++; - } - continue; - default: { - String msg = String.format( - "Invalid escape sequence: \\%c \\\\u%04X", - ch, (int) ch); - throw new IllegalArgumentException(msg); - } - } - } - - chars[to++] = ch; - } - return new String(chars, 0, to); - } -} diff --git a/module-api/src/main/resources/discord-appender.xml b/module-api/src/main/resources/discord-appender.xml deleted file mode 100644 index 74ccd739..00000000 --- a/module-api/src/main/resources/discord-appender.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - ${DISCORD_WEBHOOK_URI} - 에러났대... - https://www.greenart.co.kr/upimage/new_editor/20212/20210201112021.jpg - false - - - - - ERROR - - - \ No newline at end of file diff --git a/module-api/src/main/resources/logback-spring.xml b/module-api/src/main/resources/logback-spring.xml index 2b6fd250..fa0c7601 100644 --- a/module-api/src/main/resources/logback-spring.xml +++ b/module-api/src/main/resources/logback-spring.xml @@ -1,21 +1,16 @@ - + value="[%d{yyyy-MM-dd HH:mm:ss}:%-4relative] %green([%thread]) %highlight(%-5level) %cyan(%X{request_id}) %boldWhite([%C.%M:%yellow(%L)]) - %msg%n"/> - - - - \ No newline at end of file diff --git a/module-domain/build.gradle b/module-domain/build.gradle index 151d3be3..552fcf77 100644 --- a/module-domain/build.gradle +++ b/module-domain/build.gradle @@ -14,6 +14,9 @@ dependencies { implementation 'org.springframework.boot:spring-boot-starter-cache' implementation 'com.github.ben-manes.caffeine:caffeine:3.1.2' + // p6spy + implementation "com.github.gavlyukovskiy:p6spy-spring-boot-starter:1.9.2" + // QueryDSL Implementation implementation 'com.querydsl:querydsl-jpa:5.0.0:jakarta' annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"