diff --git a/backend/pcloud-api/src/main/java/com/api/global/slack/SlackAppender.java b/backend/pcloud-api/src/main/java/com/api/global/slack/SlackAppender.java new file mode 100644 index 00000000..b197ae52 --- /dev/null +++ b/backend/pcloud-api/src/main/java/com/api/global/slack/SlackAppender.java @@ -0,0 +1,54 @@ +package com.api.global.slack; + +import ch.qos.logback.classic.spi.ILoggingEvent; +import ch.qos.logback.core.AppenderBase; +import org.springframework.web.client.RestTemplate; + +import java.text.SimpleDateFormat; +import java.util.List; +import java.util.Map; + +public class SlackAppender extends AppenderBase { + + private static final String WEBHOOK_URL = System.getenv("SLACK_WEBHOOK_URL"); + + @Override + protected void append(final ILoggingEvent eventObject) { + RestTemplate restTemplate = new RestTemplate(); + Map body = createSlackErrorBody(eventObject); + restTemplate.postForEntity(WEBHOOK_URL, body, String.class); + } + + private Map createSlackErrorBody(final ILoggingEvent eventObject) { + String message = createMessage(eventObject); + return Map.of("channel", "#에러-발생", + "fallback", "요청을 실패했어요 :cry:", + "color", "#2eb886", + "pretext", "에러가 발생했어요 확인해주세요 :cry:", + "author_name", "pop-cloud", + "text", message, + "fields", + List.of( + Map.of( + "title", "로그 레벨", + "value", eventObject.getLevel().levelStr, + "short", false + ), + "timestamp", eventObject.getTimeStamp() + ) + ); + } + + private String createMessage(final ILoggingEvent eventObject) { + String baseMessage = "에러가 발생했습니다.\n"; + String pattern = baseMessage + "```%s %s %s [%s] - %s```"; + SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); + return String.format(pattern, + simpleDateFormat.format(eventObject.getTimeStamp()), + eventObject.getLevel(), + eventObject.getThreadName(), + eventObject.getLoggerName(), + eventObject.getFormattedMessage()); + } +} + diff --git a/backend/pcloud-api/src/main/resources/logback-spring.xml b/backend/pcloud-api/src/main/resources/logback-spring.xml new file mode 100644 index 00000000..f4d4126f --- /dev/null +++ b/backend/pcloud-api/src/main/resources/logback-spring.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + ERROR + + + + + + + + + + + diff --git a/backend/pcloud-common/build.gradle b/backend/pcloud-common/build.gradle index 74591fe7..417392c1 100644 --- a/backend/pcloud-common/build.gradle +++ b/backend/pcloud-common/build.gradle @@ -13,4 +13,7 @@ dependencies { implementation group: 'io.jsonwebtoken', name: 'jjwt-api', version: '0.11.2' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-impl', version: '0.11.2' runtimeOnly group: 'io.jsonwebtoken', name: 'jjwt-jackson', version: '0.11.2' + + // slack + implementation group: 'com.slack.api', name: 'slack-api-client', version: '1.40.3' }