Skip to content

Commit c317790

Browse files
committed
feat: add LoggingAspect
1 parent 1053e35 commit c317790

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package kattsyn.dev.rentplace.aspects;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.aspectj.lang.ProceedingJoinPoint;
5+
import org.aspectj.lang.annotation.Around;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.springframework.stereotype.Component;
8+
9+
@Slf4j
10+
@Aspect
11+
@Component
12+
public class LoggingAspect {
13+
14+
@Around("execution(public * kattsyn.dev.rentplace.services..*(..))")
15+
public Object logServiceMethods(ProceedingJoinPoint joinPoint) throws Throwable {
16+
String methodName = joinPoint.getSignature().toShortString();
17+
log.info("Вызов метода: {}", methodName);
18+
long startTime = System.currentTimeMillis();
19+
try {
20+
Object result = joinPoint.proceed();
21+
double elapsedTime = (double) (System.currentTimeMillis() - startTime) / 1000;
22+
log.info("Метод {} выполнен успешно за {} с", methodName, elapsedTime);
23+
return result;
24+
} catch (Throwable ex) {
25+
double elapsedTime = (double) (System.currentTimeMillis() - startTime) / 1000;
26+
log.error("Метод {} завершился с ошибкой за {} с. Ошибка: {}", methodName, elapsedTime, ex.getMessage());
27+
throw ex;
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)