log4j/log4j2/logback混用问题

如果不了解标题提到的几个日志框架,建议了解一下log4j/log4j2/logback简单使用说明,本节中pom依赖前面的内容;

代码地址

log4jdemo
log4j2demo
logbackdemo
logdemo


背景说明

java项目一般会选择一种日志框架,但很难保证当前项目依赖的三方组件会选用和当前项目相同的日志框架,比如当前项目使用了logback,项目依赖了ES,ES使用了log4j2日志框架,这时日志打印可能会有问题,甚至不打印日志,影响问题排查......

情景一
image.png

此时:
1.不在此项目(project)中指定日志属性配置文件,原项目(project A,project B,project C)中各自使用原生日志框架类打印日志,则用各自的日志配置文件打印日志。
2.在此项目中指定日志属性配置文件,原项目中的类则忽略原项目中的日志属性配置文件,用此项目中的日志属性配置文件。

情景二
image.png

原项目(project A,project B,project C)都使用slf4j(见“升级log4j至slf4j”和“升级log4j2至slf4j”),此项目(project)中重新定义日志属性配置文件,原项目中的类则使用log4j打印日志,启动时,控制台有告警,可以获知详细信息,如:

SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/mac2099/mvnRepository/org/slf4j/slf4j-log4j12/1.7.29/slf4j-log4j12-1.7.29.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/mac2099/mvnRepository/org/apache/logging/log4j/log4j-slf4j-impl/2.9.0/log4j-slf4j-impl-2.9.0.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/mac2099/mvnRepository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.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]
new log4j 2021-10-11 23:32:04 [INFO] com.mac2099.log4j.Main: log4j demo
new log4j 2021-10-11 23:32:04 [INFO] com.mac2099.log4j2.Main: log4j2 demo
new log4j 2021-10-11 23:32:04 [INFO] com.mac2099.logback.Main: logback demo

得知最终使用了log4j日志框架;
pom.xml




    4.0.0

    org.example
    logdemo
    1.0-SNAPSHOT

    logdemo
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
            org.example
            log4jdemo
            1.0-SNAPSHOT
        
        
            org.example
            log4j2demo
            1.0-SNAPSHOT
        
        
            org.example
            logbackdemo
            1.0-SNAPSHOT
        
    

排除掉子模块中slf4j依赖的pom




    4.0.0

    org.example
    logdemo
    1.0-SNAPSHOT

    logdemo
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
            org.example
            log4jdemo
            1.0-SNAPSHOT
            
                
                    org.slf4j
                    slf4j-log4j12
                
            
        
        
            org.example
            log4j2demo
            1.0-SNAPSHOT
            
                
                    org.apache.logging.log4j
                    log4j-slf4j-impl
                
            
        
        
            org.example
            logbackdemo
            1.0-SNAPSHOT
        
        
            junit
            junit
            4.12
            test
        
    

运行结果,全部使用了logback配置:

23:57:25,020 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
23:57:25,021 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
23:57:25,021 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/mac2099/myinfo/code/logdemo/target/classes/logback.xml]
23:57:25,021 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
23:57:25,021 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/Users/mac2099/mvnRepository/org/example/logbackdemo/1.0-SNAPSHOT/logbackdemo-1.0-SNAPSHOT.jar!/logback.xml]
23:57:25,022 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [file:/Users/mac2099/myinfo/code/logdemo/target/classes/logback.xml]
23:57:25,081 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
23:57:25,082 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
23:57:25,088 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
23:57:25,092 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
23:57:25,130 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
23:57:25,130 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[ROOT]
23:57:25,131 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
23:57:25,132 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@42f30e0a - Registering current configuration as safe fallback point

new logback2021-10-11 23:57:25 [INFO] com.mac2099.log4j.Main: log4j demo
new logback2021-10-11 23:57:25 [INFO] com.mac2099.log4j2.Main: log4j2 demo
new logback2021-10-11 23:57:25 [INFO] com.mac2099.logback.Main: logback demo

情景三
image.png

或者


image.png

如果project A使用了log4j,没有使用slf4j,则需要排除log4j并增加log4j-over-slf4j依赖,用来桥接日志打印


    org.example
    log4jdemo
    1.0-SNAPSHOT
    
        
            log4j
            log4j
        
    


    org.slf4j
    log4j-over-slf4j
    1.7.29

如果project B使用了log4j2,没有使用slf4j,则需要排除log4j2并增加log4j-to-slf4j依赖,用来桥接日志打印


    org.example
    log4j2demo
    1.0-SNAPSHOT
    
        
            org.apache.logging.log4j
            log4j-core
        
    


    org.apache.logging.log4j
    log4j-to-slf4j
    2.12.1

运行结果,全部使用了logback配置:

06:47:20,868 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback-test.xml]
06:47:20,868 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
06:47:20,868 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback.xml] at [file:/Users/mac2099/myinfo/code/logdemo/target/classes/logback.xml]
06:47:20,869 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs multiple times on the classpath.
06:47:20,869 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [jar:file:/Users/mac2099/mvnRepository/org/example/logbackdemo/1.0-SNAPSHOT/logbackdemo-1.0-SNAPSHOT.jar!/logback.xml]
06:47:20,869 |-WARN in ch.qos.logback.classic.LoggerContext[default] - Resource [logback.xml] occurs at [file:/Users/mac2099/myinfo/code/logdemo/target/classes/logback.xml]
06:47:20,944 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
06:47:20,945 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
06:47:20,950 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [console]
06:47:20,955 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
06:47:20,997 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
06:47:20,998 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [console] to Logger[ROOT]
06:47:20,998 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
06:47:20,999 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5fe5c6f - Registering current configuration as safe fallback point

new logback2021-10-12 06:47:21 [INFO] com.mac2099.log4j.Main: log4j demo
new logback2021-10-12 06:47:21 [INFO] com.mac2099.log4j2.Main: log4j2 demo
new logback2021-10-12 06:47:21 [INFO] com.mac2099.logback.Main: logback demo

升级log4j至slf4j

引入依赖




    4.0.0
    org.example
    log4jdemo
    1.0-SNAPSHOT

    log4jdemo
    
    http://www.example.com

    
        UTF-8
        1.8
        1.8
    

    
        
        
            log4j
            log4j
            1.2.17
        

        
        
            org.slf4j
            slf4j-log4j12
            1.7.29
        

        
    

Main.java

package com.mac2099.log4j;

// 使用slf4j
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Hello world!
 */
public class Main {
    static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
    public static void main(String[] args) {
        LOGGER.info("log4j demo");
    }
}

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

升级log4j2至slf4j

pom.xml



    4.0.0
    org.example
    log4j2demo
    1.0-SNAPSHOT
    
        UTF-8
        1.8
        1.8
    

    
        
            org.apache.logging.log4j
            log4j-core
            2.12.1
        
        
        
            org.apache.logging.log4j
            log4j-slf4j-impl
            2.9.0
        
    


Main.java

package com.mac2099.log4j2;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Hello world!
 */
public class Main {
    static final Logger LOGGER = LoggerFactory.getLogger(Main.class);
    public static void main(String[] args) {
        LOGGER.info("log4j2 demo");
    }
}

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

你可能感兴趣的:(log4j/log4j2/logback混用问题)