-
카테고리
-
세부 분야
백엔드
-
해결 여부
미해결
실행시간에 따른 log 레벨 적용
22.06.03 17:26 작성 조회수 258
3
강사님께서 말씀주신 실행시간에 따른 log 레벨 적용해 보았습니다.
필요하신 부분 참고부탁 드립니다.
(오류시에만 로그 남기는 부분은 @AfterThrowing을 사용하면 될것같습니다, - retry 에서 재시도 부분 제거)
1. 애노테이션 - 기본값 1000
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckTime {
int value() default 1000;
}
2. Aspect
@Around("@annotation(checkTime)")
public void checkTimer(ProceedingJoinPoint joinPoint, CheckTime checkTime) throws Throwable {
int mills = checkTime.value();
int maxMills = 4000;
StopWatch stopWatch = new StopWatch();
stopWatch.start();
joinPoint.proceed();
stopWatch.stop();
long totalTimeMillis = stopWatch.getTotalTimeMillis();
Signature signature = joinPoint.getSignature();
String methodName = signature.getName();
if (totalTimeMillis <= mills) {
log.info("methodName: {}, 실행시간 = {}ms", methodName, totalTimeMillis);
} else if (totalTimeMillis <= maxMills) {
log.warn("methodName: {}, 실행시간 = {}ms", methodName, totalTimeMillis);
} else {
log.error("methodName: {}, 실행시간 = {}ms", methodName, totalTimeMillis);
}
}
3. 애플리케이션 코드 - 랜덤으로 실행시간 적용
@CheckTime(value = 2000)
public String save(String itemId) {
seq++;
if (seq % 5 == 0) {
throw new IllegalStateException("예외 발생");
}
sleep();
return "ok";
}
private void sleep() {
int[] ints = {1000, 2000,3000,4000,5000};
double random= Math.random();
int num = (int) Math.round(random * (ints.length-1));
System.out.println(ints[num]);
try {
Thread.sleep(ints[num]);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
답변을 작성해보세요.
0
답변 1