package com.dev.log;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Logging {
}
소스 설명
종류
매개변수
RetentionPolicy (Retention 어노테이션 메모리 정책)
package com.dev.log;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Stream;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@RequiredArgsConstructor
@Aspect
@Component
public class LoggingAop {
Logger log = LoggerFactory.getLogger(LoggingAop.class);
@Before(value = "@annotation(logging)", argNames="joinPoint, logging")
public void log(JoinPoint joinPoint, Logging logging) throws Throwable{
geValue(joinPoint, logging);
}
void geValue(JoinPoint joinPoint, Logging logging) {
log.info(">>>>> " + getClassName(joinPoint, logging) + " / " + getMethodName(joinPoint) + " / " + getParameter(joinPoint));
}
// 메소드명 가져오기
public String getMethodName(JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
return method.getName();
}
// 클래스명 가져오기
public String getClassName(JoinPoint joinPoint, Logging logging) {
Class<?> activeClass = joinPoint.getTarget().getClass();
logging= activeClass.getAnnotation(Logging.class);
return activeClass.getName();
}
// 매개변수 가져오기
public List<String> getParameter(JoinPoint joinPoint) {
List<String> parameterNames = new ArrayList<>();
Stream.of(joinPoint.getArgs()).forEach(it -> {
parameterNames.add(it.toString());
});
return parameterNames;
}
}
소스 설명
[SpringBoot] 1. MVC 설정하기 (0) | 2021.07.20 |
---|---|
스프링 부트에서 properties 값 받기 (0) | 2021.07.03 |
[Java] DB Connection Pool (DB 커넥션 풀 / DBCP) (0) | 2021.06.17 |
[Spring] 컨테이너(Container) (0) | 2021.05.29 |
[Spring] 스프링이란 (0) | 2021.05.28 |
댓글 영역