使用maven引入slf4j、logback时发生冲突

pom.xml文件中的slf4j、logback引入配置:

 1     <!-- 日志 -->
 2 
 3     <dependency>
 4       <groupId>org.slf4j</groupId>
 5       <artifactId>slf4j-log4j12</artifactId>
 6       <version>1.7.12</version>
 7     </dependency>
 8 
 9     <dependency>
10         <groupId>ch.qos.logback</groupId>
11         <artifactId>logback-core</artifactId>
12         <version>1.1.1</version>
13     </dependency>
14 
15     <dependency>
16         <groupId>ch.qos.logback</groupId>
17         <artifactId>logback-classic</artifactId>
18         <version>1.1.1</version>
19     </dependency>

logback.xml中的配置:

 1 <configuration>
 2 
 3   <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
 4     <!-- encoders are assigned the type
 5          ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
 6     <encoder>
 7       <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
 8     </encoder>
 9   </appender>
10 
11   <root level="debug">
12     <appender-ref ref="STDOUT" />
13   </root>
14 
15 </configuration>

运行代码时的输出:

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/JAVA/MavenLocalRepository/repository/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/JAVA/MavenLocalRepository/repository/ch/qos/logback/logback-classic/1.1.1/logback-classic-1.1.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]

显示的是这两个jar冲突了。

解决方案:

将pom.xml中的org.slf4j和logback-core去掉,仅留下logback-classic就可以了

<!-- 日志 -->
    <!-- 
        <dependency>
          <groupId>org.slf4j</groupId>
          <artifactId>slf4j-log4j12</artifactId>
          <version>1.7.12</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.1.1</version>
        </dependency>
     -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.1</version>
    </dependency>

正常输出:

省略........

信息: Refreshing org.springframework.context.support.GenericApplicationContext@66d1af89: startup date [Sun Mar 04 13:18:26 CST 2018]; root of context hierarchy
三月 04, 2018 1:18:26 下午 com.mchange.v2.log.MLog <clinit>
信息: MLog clients using java 1.4+ standard logging.
三月 04, 2018 1:18:27 下午 com.mchange.v2.c3p0.C3P0Registry banner
信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
13:18:27.974 [main] DEBUG org.apache.ibatis.logging.LogFactory - Logging initialized using 'class org.apache.ibatis.logging.slf4j.Slf4jImpl' adapter.
13:18:28.312 [main] DEBUG org.apache.ibatis.io.VFS - Class not found: org.jboss.vfs.VFS
13:18:28.312 [main] DEBUG org.apache.ibatis.io.JBoss6VFS - JBoss 6 VFS API is not available in this environment.
13:18:28.315 [main] DEBUG org.apache.ibatis.io.VFS - Class not found: org.jboss.vfs.VirtualFile
13:18:28.317 [main] DEBUG org.apache.ibatis.io.VFS - VFS implementation org.apache.ibatis.io.JBoss6VFS is not valid in this environment.

省略......

原文地址:https://www.cnblogs.com/Drajun/p/8504465.html