How to load the custom Spring AOP Logging Library to My MicroService

26 Views Asked by At

I need your help in resolving the issue i am currently facing.

I have created a custom logging library using Spring AOP. However, when it as a dependency to my microservice. The AOP custom library executions are skipped. Can someone please help me know to resolve this issue. below is the code snippet.

AOP Custom Library:

@Component
@Aspect
@Slf4j
public class LogAspect {

@Around("@annotation(com.lib.unifiedlog.aspect.annotations.LogGenerator)")
  public Object log(ProceedingJoinPoint point) throws Throwable {
    var codeSignature = (CodeSignature) point.getSignature();
    var methodSignature = (MethodSignature) point.getSignature();
    String className = methodSignature.getDeclaringType().getSimpleName();
    Method method = methodSignature.getMethod();

    var annotation = method.getAnnotation(LogGenerator.class);
    LogLevel level = annotation.value();
    ChronoUnit unit = annotation.unit();
    boolean showArgs = annotation.showArgs();
    boolean showResult = annotation.showResult();
    boolean mask = annotation.mask();
    boolean showExecutionTime = annotation.showExecutionTime();
    String methodName = method.getName();
    Object[] methodArgs = point.getArgs();
    String[] methodParams = codeSignature.getParameterNames();

    var start = Instant.now();
    var response = point.proceed();
    var end = Instant.now();
    var duration = String.format("%s %s", Duration.between(start, end).toMillis(), unit.name().toLowerCase());

   

    return response;
  }   
}

Below is my LogGenerator Annotation interface

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface LogGenerator {

}

I want to add the above peace of code as a maven dependency to my User microservice.

I have my Microservice GetMethod of my controller when i want to use this to log messages.

@RestController
public class UserController Implements UserApi{
@Override
  @LogGenerator(showArgs = true, showResult = true, unit = ChronoUnit.MILLIS, mask = true)
  public ResponseEntity<List<User>> getusers() {
    List<User> response = new ArrayList<>();
    List<User> users= adapter.getUser();
    return ResponseEntity.ok(response);
  }
}

Below is my Spring boot application runner class.

@SpringBootApplication
public class UserApplication{
public static void main(String[] args) {
        SpringApplication.run(UserApplication.class, args);
    }
0

There are 0 best solutions below