首先说结论:
配置logback.xml的时候,注意在引入依赖时中的<scope>标签中的值,若为test,则在Sources中的代码是引不到依赖的
就是这里引不到;而且 引入了logback-classic这个包,在引入slf4j-simple这个包 可能会出现jar包冲突,我是这个类StaticLoggerBinder.class发生了冲突,因为这个类在两个包中都有了。
排错记录
IDEA 配置logback 引入
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-core -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.2.3</version>
</dependency>
<!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
代码
package com.log.logback.one;
import ch.qos.logback.core.util.StatusPrinter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class LogMain {
private final static Logger LOG = LoggerFactory.getLogger(LogMain.class);
public static void main(String[] args) {
// LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
// StatusPrinter.print(lc);
LOG.trace("Hello world!");
LOG.debug("How are you today?");
LOG.info("i am fine");
LOG.warn("I love programming.");
LOG.error("i am programming");
}
}
输出如下
百度过程中发现 自己缺少了这个包
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
于是引入
输出如下
首先看下logback.xml配置
这与预期不符,输出的内容应该是从LOG.trace("Hello World")开始输出,而且输出的格式也很奇怪
后来发现在
pom.xml中的
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
<scope>test</scope>
</dependency>
这段代码中的<scope>test</scope>没有去掉
百度一下 发现<scope>标签 应该标识了 依赖的作用域
我的pom.xml代码中直接在maven镜像市场中复制,<scope>test<scope>没有去掉
这会导致在
这个包里面无法引用这些依赖。
于是,去掉<scope>test<scope>这一行代码 或者换成<scope>compile<scope>(默认范围)
运行结果如下
哦吼
重点看 contains multiple SLF4J bindings
依赖冲突了。之前百度搜索到的结果是 需要加入slf4j-simple这个包,但是logback-classic中已经有StaticLoggerBinder.class了
两个包中的StaticLoggerBinder.class冲突了
去掉slf4j-simple
运行结果如下
nice,搞定