Skip to content

Commit

Permalink
Update: logback 설정 변경 및 discord 메시지 배포환경에서만 적용
Browse files Browse the repository at this point in the history
  • Loading branch information
BYEONGRYEOL committed Apr 29, 2024
1 parent a406d02 commit 01ccf74
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 12 deletions.
43 changes: 43 additions & 0 deletions src/main/java/com/gt/genti/aop/EnvAop.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.gt.genti.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import com.gt.genti.config.auth.UserDetailsImpl;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Aspect
@Component
@Slf4j
@RequiredArgsConstructor
public class EnvAop {

private final Environment env;

@Before("@annotation(com.gt.genti.aop.annotation.CheckUserIsQuit) && args(userDetailsImpl)")
public void checkUserIsQuit(final UserDetailsImpl userDetailsImpl) {
log.info("유저탈퇴확인aop실행");
if (!userDetailsImpl.isEnabled()) {
throw new RuntimeException("탈퇴한 사용자입니다.");
}
}

@Around("@annotation(com.gt.genti.aop.annotation.DeployOnly)")
public Object deployOnly(ProceedingJoinPoint joinPoint) throws Throwable {
String[] activeProfiles = env.getActiveProfiles();
for (String profile : activeProfiles) {
if ("deploy".equals(profile)) {
return joinPoint.proceed();
}
}

return null;

}
}
12 changes: 12 additions & 0 deletions src/main/java/com/gt/genti/aop/annotation/DeployOnly.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.gt.genti.aop.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface DeployOnly {

}
7 changes: 2 additions & 5 deletions src/main/java/com/gt/genti/config/auth/UserDetailsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public UserDetailsImpl(Long authId, String roles, String userEmail, String userP
this.email = userEmail;
this.password = userPw;
this.emailVerified = emailVerified;
this.locked = !locked;
this.locked = locked;
}

private Collection<GrantedAuthority> createAuthorities(String roles) {
Expand Down Expand Up @@ -153,10 +153,7 @@ public boolean isCredentialsNonExpired() {
*/
@Override
public boolean isEnabled() {
//이메일이 인증되어 있고 계정이 잠겨있지 않으면 true
//상식과 조금 벗어나서, Customizing 하였음
return (emailVerified && locked);

return true;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.stereotype.Service;

import com.gt.genti.aop.annotation.DeployOnly;
import com.gt.genti.external.discord.restclient.DiscordRestClient;

import jakarta.annotation.PostConstruct;
Expand All @@ -23,6 +24,7 @@ void init() {
sb = new StringBuilder(jsonTemplate);
}

@DeployOnly
public void sendToDiscord(String message) {

int startIndex = sb.lastIndexOf("$1");
Expand Down
23 changes: 17 additions & 6 deletions src/main/resources/logback-spring.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
<tts>false</tts>
</appender>

<appender name="Console" class="ch.qos.logback.core.ConsoleAppender">
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
<charset>utf8</charset>
</encoder>
</appender>

<appender name="File" class="ch.qos.logback.core.FileAppender">
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>/logs/app.log</file>
<encoder>
<Pattern>${CONSOLE_LOG_PATTERN}</Pattern>
Expand All @@ -35,8 +35,19 @@
</filter>
</appender>

<root level="INFO">
<appender-ref ref="ASYNC_DISCORD"/>
<appender-ref ref="Console"/>
</root>
<!-- <root level="INFO">-->
<!-- <appender-ref ref="CONSOLE"/>-->
<!-- </root>-->
<springProfile name="local">
<root level="INFO">
<appender-ref ref="CONSOLE"/>
</root>
</springProfile>
<springProfile name="deploy">
<root level="INFO">
<appender-ref ref="ASYNC_DISCORD"/>
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</root>
</springProfile>
</configuration>
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
package com.gt.genti.service;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;

import lombok.extern.slf4j.Slf4j;

@SpringBootTest
@Slf4j
public class DiscordLogTest {
@ActiveProfiles("local")
public class DiscordLog_LocalEnvTest {
@Test
@DisplayName("로컬환경이므로 디스코드에 전송되지않는것이 정상")
void loggingError() {
log.error("오류");
log.warn("워닝");
Expand Down

0 comments on commit 01ccf74

Please sign in to comment.