-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
backend/pcloud-api/src/main/java/com/api/global/slack/SlackAppender.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,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<ILoggingEvent> { | ||
|
||
private static final String WEBHOOK_URL = System.getenv("SLACK_WEBHOOK_URL"); | ||
|
||
@Override | ||
protected void append(final ILoggingEvent eventObject) { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
Map<String, Object> body = createSlackErrorBody(eventObject); | ||
restTemplate.postForEntity(WEBHOOK_URL, body, String.class); | ||
} | ||
|
||
private Map<String, Object> 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()); | ||
} | ||
} | ||
|
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,30 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<configuration scan="true" scanPeriod="60 seconds"> | ||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/> | ||
<property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/> | ||
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/> | ||
<include resource="org/springframework/boot/logging/logback/file-appender.xml"/> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="FILE"/> | ||
<appender-ref ref="CONSOLE"/> | ||
</root> | ||
|
||
<appender name="SLACK_APPENDER" class="com.api.global.slack.SlackAppender"> | ||
</appender> | ||
|
||
<appender name="ASYNC_SLACK_APPENDER" class="ch.qos.logback.classic.AsyncAppender"> | ||
<filter class="ch.qos.logback.classic.filter.ThresholdFilter"> | ||
<level>ERROR</level> | ||
</filter> | ||
<appender-ref ref="SLACK_APPENDER"/> | ||
</appender> | ||
|
||
<springProfile name="!local"> | ||
<logger name="com.api" level="DEBUG" additivity="true"> | ||
<appender-ref ref="ASYNC_SLACK_APPENDER"/> | ||
</logger> | ||
</springProfile> | ||
|
||
</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