728x90
Spring mvc 환경에서 aop 를 이용해
logging을 컨트롤러에서
함수실행시 request와 결과값을 log로 남겨주었다.
>환경 : 전자정부프레임워크 3.10 , Spring 4.3.25
1. AOP를 위한 설정
# IN pom.xml
<!-- aop 위해 추가-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.3.25.RELEASE</version>
</dependency>
<!-- AspectJ -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.14</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.9.9</version>
</dependency>
aop 와 aspectj 언어를 사용하기위해 의존성을 추가한다.
# IN dispatcher-servlet.xml
<!-- aop설정을위해 추가-->
<aop:aspectj-autoproxy/>
2. AOP 구현
# loggingAspect class 작성 : 이곳에서 aop 함수를 작성한다.
package idmcs.api.pub.rrc.provider.requestRrcQr.common;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.persistence.criteria.JoinType;
import java.util.Arrays;
@Aspect
@Component
public class LoggingAspect {
public static final String FONT_GREEN = "\u001B[32m";
public static final String RESET = "\u001B[0m";
public static final String FONT_BLUE = "\u001B[34m";
private static final Logger logger = LoggerFactory.getLogger(LoggingAspect.class);
@Around("execution(* idmcs.api.pub.rrc.provider.requestRrcQr.controller..*(..))")
public Object logPrint(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object result = proceedingJoinPoint.proceed();
Object[] args = proceedingJoinPoint.getArgs();
String type = proceedingJoinPoint.getSignature().getDeclaringTypeName();
String name = "";
if (type.contains("Controller")) {
name = "[Request]Controller : ";
} else if (type.contains("Service")) {
name = "Service : ";
} else if (type.contains("VO")) {
name = "Persistence : ";
}
long end = System.currentTimeMillis();
logger.info(FONT_GREEN + "■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■");
logger.info(name + type + "."+proceedingJoinPoint.getSignature().getName() + "()");
logger.info("Argument/Parameter : " + Arrays.toString(proceedingJoinPoint.getArgs()));
logger.info("----------------------------------------------------------------");
logger.info(FONT_BLUE+"[Response] : {}({}) = {} ({}ms)", proceedingJoinPoint.getSignature().getDeclaringTypeName(),
proceedingJoinPoint.getSignature().getName(), result,(end-start));
logger.info("■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■" + RESET);
return result;
}
}
지금은 controller 에서만 잡아놓고 있지만 service및 vo에서 잡을수도 있도록하려고 name설정을 여러개를 넣어둠.
3. 실행결과
728x90
'Back > Spring' 카테고리의 다른 글
@Component , @Autowired ?? 사용하는 이유 (0) | 2022.09.01 |
---|---|
Spring] AOP LOG 파일에 저장하기 (0) | 2022.08.29 |
Spring Mvc 기반 api 예외처리 (0) | 2022.08.26 |
Spring ajax , list <map> 으로 데이터 넘기기 (0) | 2022.08.16 |
Thymeleaf 와 jsp의 차이점 (0) | 2022.08.16 |