@Slf4j的日志功能不起作用

引入lombok

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.16</version>
    </dependency>

代码中使用

在类上添加@Slf4j注解,就可以在类中使用log变量

@Aspect
@Slf4j
public class myaspect {

    @Pointcut("execution(* site.yalong.controller..*.*(..))")
    public void webLog() {
    }

    @Before("webLog()")
    public void doBefore(JoinPoint joinPoint) throws Throwable {
        // 接收到请求,记录请求内容
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes != null) {
            HttpServletRequest request = attributes.getRequest();
            // 记录下请求内容
            System.out.println("in");
            log.info("\n\n有人访问了:{}\nARGS :{}\n", request.getRequestURL().toString(), Arrays.toString(joinPoint.getArgs()));
        }

    }

    @AfterReturning(returning = "object", pointcut = "webLog()")
    public void doAfterReturning(Object object) throws Throwable {
        // 处理完请求,返回内容
        log.info("RESPONSE : " + object);
    }
}

添加slf4依赖

注意,此时没有slf4依赖,所以不可以使用log.xxx()方法

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
    </dependency>

添加logback-classic依赖

此时虽然代码不报错,但是日志功能失效,缺少日志记录工具
完整依赖如下:

    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.16</version>
    </dependency>

    <!--日志-->
    <!--logback-classic包含了slf4j-->
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>
posted @ 2021-03-31 12:01  rm-rf*  阅读(1170)  评论(0编辑  收藏  举报