一、问题描述
在 Spring Boot
项目中,采用父工程管理子模块的模型来开发项目的过程中发现,在未引入 spring-boot-dependencies
依赖的 common
模块中使用 @Slf4j
注解不能出现 log.info
等函数调用。
二、问题分析
-
slf4J
是众多接口的集合,它不负责具体的日志实现,只在编译时负责寻找合适的日志系统进行绑定,具体有哪些接口,全部都定义在slf4j-api
中 -
Lombok
只是一个插件,封装了log
的getter
和setter
方法,可以直接使用log
来输出日志信息 -
slf4j-log4j12
是链接slf4j-api
和log4j
中间的适配器,它实现了slf4j-api
中的StaticLoggerBinder
接口,从而使得在编译时绑定等是slf4j-log4j12
的getSingleton
方法
三、解决方案
-
在模块中
pom
文件中新增如下依赖<!-- 解决 Lombok @Slf4j 中 log.info 无效的问题 --> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <!-- 无需指定版本是因为指定了 spring-boot-dependencies 的版本,而该版本指定了 slf4j 的版本 --> <!--<version>1.7.30</version>--> </dependency>
-
无需指定版本是因为指定了
spring-boot-dependencies
的版本,而该版本指定了slf4j
的版本<!-- Spring Boot 固定版本,未经评估不能更改 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.2.5.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency>