-
Notifications
You must be signed in to change notification settings - Fork 2
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
74 additions
and
1 deletion.
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
65 changes: 65 additions & 0 deletions
65
src/main/java/com/moing/backend/global/util/SlackService.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,65 @@ | ||
package com.moing.backend.global.util; | ||
|
||
import com.slack.api.Slack; | ||
import com.slack.api.model.Attachment; | ||
import com.slack.api.model.Field; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
import javax.servlet.http.HttpServletRequest; | ||
import java.io.IOException; | ||
import java.time.LocalDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
import java.util.List; | ||
|
||
import static com.slack.api.webhook.WebhookPayloads.payload; | ||
|
||
@Service | ||
@Slf4j | ||
public class SlackService { | ||
|
||
@Value("${webhook.slack.error_url}") | ||
private String errorWebhookUrl; | ||
|
||
private final Slack slackClient = Slack.getInstance(); | ||
|
||
// 슬랙 에러 알림 보내는 메서드 | ||
public void sendSlackAlertErrorLog(Exception e, HttpServletRequest request) { | ||
try { | ||
slackClient.send(errorWebhookUrl, payload(p -> p | ||
.text("[500 에러가 발생했습니다.]") | ||
.attachments( | ||
List.of(generateSlackAttachment(e, request)) | ||
) | ||
)); | ||
} catch (IOException slackError) { | ||
log.debug("Slack 통신과의 예외 발생"); | ||
} | ||
} | ||
|
||
// attachment 생성 메서드 | ||
private Attachment generateSlackAttachment(Exception e, HttpServletRequest request) { | ||
String requestTime = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").format(LocalDateTime.now()); | ||
String xffHeader = request.getHeader("X-FORWARDED-FOR"); | ||
return Attachment.builder() | ||
.color("ff0000") | ||
.title(requestTime + " 발생 에러 로그") | ||
.fields(List.of( | ||
generateSlackField("Request IP", xffHeader == null ? request.getRemoteAddr() : xffHeader), | ||
generateSlackField("Request URL", request.getRequestURL() + " " + request.getMethod()), | ||
generateSlackField("Error Message", e.getMessage()) | ||
) | ||
) | ||
.build(); | ||
} | ||
|
||
private Field generateSlackField(String title, String value) { | ||
return Field.builder() | ||
.title(title) | ||
.value(value) | ||
.valueShortEnough(false) | ||
.build(); | ||
} | ||
|
||
} |