java-log4j日志冲突解决

一、概述

java日志框架较多,其中主流的slf4j和commons-logging是日志接口,log4j、log4j2和logback是真正的日志实现库。
java-log4j日志冲突解决_第1张图片

二、具体库单独使用

2.1 log4j

<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);

2.2 log4j2

<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);

2.3 logback

<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接口

三、具体库实现slf4j标准

3.1 log4j

java-log4j日志冲突解决_第2张图片

 <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);

3.2 log4j2

java-log4j日志冲突解决_第3张图片

<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);

3.3 logback

logback自身已经实现了slf4j接口

四、多依赖项目的日志统一

项目可能有很多依赖,各个依赖有可能有着各不相同的日志实现方式。比如我们有五个依赖,他们分别是:

  1. 独立log4j
  2. 独立log4j2
  3. slf4j化log4j
  4. slf4j化log4j2
  5. slf4j化logback

4.1 各依赖都实现了slf4j,指定具体库

java-log4j日志冲突解决_第4张图片
项目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>

4.2 有的依赖实现了slf4j,有的依赖使用的是具体库,指定一种具体库

java-log4j日志冲突解决_第5张图片
同样,项目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>

你可能感兴趣的:(java,java,log4j)