java日志框架较多,其中主流的slf4j和commons-logging是日志接口,log4j、log4j2和logback是真正的日志实现库。
<dependency>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
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.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.12.1version>
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.logbackgroupId>
<artifactId>logback-classicartifactId>
<version>1.2.3version>
dependency>
classpath下logback.xml
<configuration>
<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%p] %c: %m%npattern>
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>log4jgroupId>
<artifactId>log4jartifactId>
<version>1.2.17version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.29version>
dependency>
使用:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
...
static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.12.1version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
<version>2.9.0version>
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.examplegroupId>
<artifactId>log4j-testartifactId>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.examplegroupId>
<artifactId>log4j2-testartifactId>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-slf4j-implartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.examplegroupId>
<artifactId>logback-testartifactId>
<version>1.0-SNAPSHOTversion>
dependency>
同样,项目project还是统一使用logback。
解决办法:用log4j-over-slf4j替换log4j,用log4j-to-slf4j替换log4j2,并在项目project中提出log4j和log4j2。
<dependency>
<groupId>org.examplegroupId>
<artifactId>log4j-testartifactId>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>log4jgroupId>
<artifactId>log4jartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>log4j-over-slf4jartifactId>
<version>1.7.29version>
dependency>
<dependency>
<groupId>org.examplegroupId>
<artifactId>log4j2-testartifactId>
<version>1.0-SNAPSHOTversion>
<exclusions>
<exclusion>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
exclusion>
exclusions>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-to-slf4jartifactId>
<version>2.12.1version>
dependency>
<dependency>
<groupId>org.examplegroupId>
<artifactId>logback-testartifactId>
<version>1.0-SNAPSHOTversion>
dependency>