Skip to content

Commit

Permalink
feat: 슬랙 로그 알림 추가 (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
sosow0212 authored Aug 7, 2024
1 parent 54bfa8e commit d7f52dc
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
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());
}
}

30 changes: 30 additions & 0 deletions backend/pcloud-api/src/main/resources/logback-spring.xml
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>
3 changes: 3 additions & 0 deletions backend/pcloud-common/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}

0 comments on commit d7f52dc

Please sign in to comment.