【问题标题】:I have to add logging to a method in a series of controllers in SpringBoot Is there a way to do the logging without adding to the method我必须在 SpringBoot 的一系列控制器中的方法中添加日志记录有没有办法在不添加到方法的情况下进行日志记录
【发布时间】:2022-01-08 14:14:02
【问题描述】:

我有一系列具有控制器的微服务,每个控制器都有一个方法

  @SneakyThrows
  @PostMapping(value = "/getData", produces = { "application/json" })
  public GetDataResponse getData(@RequestBody GetDataRequest data) {

  }

我可以向每个控制器添加日志记录,以便在调用每个控制器时获取相同的数据。

  @SneakyThrows
  @PostMapping(value = "application/<controllerApplicationName>/getData", produces = { "application/json" })
  public GetDataResponse getData(@RequestBody GetDataRequest data) {
      log.info("From client "+data.getClientId()

  }

有没有办法让springboot来处理这个日志,所以我不需要为每个控制器和新控制器添加日志

【问题讨论】:

    标签: spring-boot logging


    【解决方案1】:

    是的,有办法做到这一点,您可以将 Spring AOP 添加到您的项目中, AOP 是一种代理机制,它将拦截对 Spring 托管类中的方法的调用。但它仅适用于公共方法。

    有了这个,你不需要在每个方法中编写日志代码,对于你的整个应用程序,一个类就可以工作。

    这是一个启动示例代码。 (请务必在线参考其教程)

    package com.demo.survey.aop;
    
    import javax.annotation.Resource;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.aspectj.lang.reflect.MethodSignature;
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Component;
    
    @Aspect
    @Component
    public class LoggingAspect {
        
        @Pointcut("execution(* com.demo.controllers.*.*(..))")
        private void forControllerPackage() {}
        
        @Pointcut("execution(* com.demo.serviceImpl.*.*(..))")
        private void forServiceImplPackage() {}
        
        @Pointcut("forControllerPackage() || forServiceImplPackage()")
        private void applyPointCut() {}
        
        // Will get called before your method gets executed
        @Before("execution(* com.demo.controllers.demoController.addData(..)))")
        public void beforeAddQuestion(JoinPoint jt) {
            MethodSignature methodSig = (MethodSignature) jt.getSignature();
            System.out.println("methodSig = "+methodSig);
            
            Object[] args = jt.getArgs();
            for(Object tempArgs : args) {
                System.out.println("tempArgs = "+tempArgs);
            }
            
        }
        
        // @Around:- This will get called before and after your method runs.
        @Around("applyPointCut()")
        public Object logExceptions(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
            Object result = null;
            //display Method Signature
            MethodSignature methodSig = (MethodSignature) proceedingJoinPoint.getSignature();
            try {
                // execute the method
                // do something before proceeding
                
                System.out.println("methodSig = "+methodSig);
            
                Object[] args = proceedingJoinPoint.getArgs();
                for(Object tempArgs : args) {
                    // Arguments which are being passed to method
                    System.out.println("tempArgs = "+tempArgs);
                }
                
                result = proceedingJoinPoint.proceed();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return result;
        } 
    }
    
    
    // Here are the 2 dependencies that you will need
    
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-aop</artifactId>
            </dependency>
            <dependency>
                <groupId>org.aspectj</groupId>
                <artifactId>aspectjtools</artifactId>
            </dependency>
    

    【讨论】:

    • 谢谢,我在考虑 AOP,但我读到它已经失宠,因为框架已经接管了很多横切关注点。也忘了提到这些控制器在不同的微服务中
    • @Tony 然后阅读有关 spring cloud 和 eureka 服务器的信息,它可能会有所帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 2021-10-13
    • 1970-01-01
    • 1970-01-01
    • 2014-09-23
    相关资源
    最近更新 更多