하고 싶은 것
- 해당 어노테이션을 가지고 있는 로그를 자동화 해서 찍기
- 이걸 좀 더 간편화해서 사용하는게 좋을 듯 해서 만듬.
AOP 메이븐 추가
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.4.2</version>
</dependency>
커스텀 어노테이션 추가
import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface LogAnnotation {
String logText() default "프로세스";
}
로그 Aspect 추가
import com.coinplug.albatross.logger.ActivityLogger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Component
@Aspect
public class LogAspect {
private static final String start = "시작";
private static final String end = "종료";
private static final String error = "오류 = {}";
@Around("@annotation(LogAnnotation)")
public Object doSchedulerLog(ProceedingJoinPoint joinPoint) throws Throwable {
//String methodName = joinPoint.getSignature().getName();
String fullName = joinPoint.getSignature().getDeclaringTypeName();
MethodSignature signature = (MethodSignature)joinPoint.getSignature();
Method method = signature.getMethod();
String logText = method.getAnnotation(LogAnnotation.class).logText();
Object obj = null;
try {
logBatchInfo(getMessage(fullName, logText, start));
obj = joinPoint.proceed();
} catch (Exception ex) {
logBatchInfo(getMessage(fullName, logText, error), ex.getMessage());
} finally {
logBatchInfo(getMessage(fullName, logText, end));
}
return obj;
}
private void logBatchInfo(String pattern, Object... args) {
ActivityLogger.batchInfo(pattern, args);
}
private String getMessage(String fullName, String logText, String typeString) {
return fullName.concat(" ").concat(logText).concat(" ").concat(typeString);
}
}
*************** ActivityLogger.batchInfo(pattern, args)는 로그 찍는 형식이라 새로 만들던가 해야 함.
실제 스케쥴러에서 사용
@Scheduled(fixedDelay = 10 * 1000)
@LogAnnotation(logText = "프로세스!!")
public void UpdateConfiguration() throws Exception {
}
'Java' 카테고리의 다른 글
자바 Json Parser (0) | 2021.02.22 |
---|---|
자바 Log Class (0) | 2021.02.22 |
자바 외부 dll 연동하기 opencv (0) | 2021.01.20 |
자바 Async3 (0) | 2020.11.08 |
자바 Async2 (0) | 2020.11.08 |