-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingAspect.java
More file actions
51 lines (40 loc) · 1.82 KB
/
Copy pathLoggingAspect.java
File metadata and controls
51 lines (40 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package in.newdevpoint.bootcamp.aop;
import in.newdevpoint.bootcamp.repository.ExceptionLogRepository;
import lombok.AllArgsConstructor;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
@Aspect
@Component
@AllArgsConstructor
public class LoggingAspect {
private final ExceptionLogRepository exceptionLogRepository;
@Around("execution(* in.newdevpoint.bootcamp.controller.*.*(..))") // Pointcut expression
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long startTime = System.currentTimeMillis();
// Proceed with the method execution
Object proceed = joinPoint.proceed();
long timeTaken = System.currentTimeMillis() - startTime;
System.out.println(joinPoint.getSignature() + " executed in " + timeTaken + " ms");
return proceed;
}
@Before("execution(* in.newdevpoint.bootcamp.service.UserService.*(..))") // Pointcut expression
public void logBeforeMethod() {
System.out.println("Before method execution");
}
@After("execution(* in.newdevpoint.bootcamp.service.UserService.*(..))")
public void logAfterMethod() {
System.out.println("After method execution");
}
/*@AfterThrowing(pointcut = "execution(* in.newdevpoint.bootcamp..*(..))", throwing = "exception")
public void logException(JoinPoint joinPoint, Throwable exception) {
exception.printStackTrace();
ExceptionLog log = new ExceptionLog();
log.setExceptionMessage(exception.getMessage());
log.setStackTrace(Arrays.toString(exception.getStackTrace()));
log.setMethodName(joinPoint.getSignature().getName());
log.setClassName(joinPoint.getSignature().getDeclaringTypeName());
log.setTimestamp(LocalDateTime.now().format(DateTimeFormatter.ISO_DATE_TIME));
exceptionLogRepository.save(log);
}*/
}