java日志框架较多,其中主流的slf4j和commons-logging是日志接口,log4j、log4j2和logback是真正的日志实现库。
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
classpath下配置文件log4j.properties
log4j.rootLogger=INFO,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%n
使用:
import org.apache.log4j.Logger;
...
Logger LOGGER = Logger.getLogger(Main.class);
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
classpath下log4j2.properties
rootLogger.level= info
rootLogger.appenderRef.stdout.ref= STDOUT
appender.console.type= Console
appender.console.name= STDOUT
appender.console.layout.type= PatternLayout
appender.console.layout.pattern= %d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%n
使用:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
...
Logger LOGGER = LogManager.getLogger(Main.class);
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.3</version>
</dependency>
classpath下logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%n</pattern>
</encoder>
</appender>
<root level="debug">
<appender-ref ref="console" />
</root>
</configuration>
使用:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
Logger LOGGER = LoggerFactory.getLogger(Main.class);
值得注意的是,logback自身已经实现了slf4j接口
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.29</version>
</dependency>
使用:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.9.0</version>
</dependency>
使用:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
logback自身已经实现了slf4j接口
项目可能有很多依赖,各个依赖有可能有着各不相同的日志实现方式。比如我们有五个依赖,他们分别是:
项目project有三个依赖,分别是log4j-test、log4j2-test、logback-test,而且它们都已经实现了slf4j接口。那么此时项目project会发现slf4j有多个binding:
解决办法:比如只用logback来打印,那么需要把log4j和log4j2的桥接给剔除掉
<dependency>
<groupId>org.example</groupId>
<artifactId>log4j-test</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>log4j2-test</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>logback-test</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
同样,项目project还是统一使用logback。
解决办法:用log4j-over-slf4j替换log4j,用log4j-to-slf4j替换log4j2,并在项目project中提出log4j和log4j2。
<dependency>
<groupId>org.example</groupId>
<artifactId>log4j-test</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<version>1.7.29</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>log4j2-test</artifactId>
<version>1.0-SNAPSHOT</version>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-to-slf4j</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.example</groupId>
<artifactId>logback-test</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>