log4j/log4j2/logback混用問題

如果不了解標(biāo)題提到的幾個日志框架帅涂,建議了解一下log4j/log4j2/logback簡單使用說明退敦,本節(jié)中pom依賴前面的內(nèi)容助析;

代碼地址

log4jdemo
log4j2demo
logbackdemo
logdemo


背景說明

java項目一般會選擇一種日志框架缠导,但很難保證當(dāng)前項目依賴的三方組件會選用和當(dāng)前項目相同的日志框架朴恳,比如當(dāng)前項目使用了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

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>logdemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>logdemo</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>log4jdemo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>log4j2demo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>logbackdemo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
    </dependencies>
</project>

排除掉子模塊中slf4j依賴的pom

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>logdemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>logdemo</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.example</groupId>
            <artifactId>log4jdemo</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>log4j2demo</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>logbackdemo</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
</project>

運行結(jié)果,全部使用了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依賴,用來橋接日志打印

<dependency>
    <groupId>org.example</groupId>
    <artifactId>log4jdemo</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>

如果project B使用了log4j2告嘲,沒有使用slf4j错维,則需要排除log4j2并增加log4j-to-slf4j依賴,用來橋接日志打印

<dependency>
    <groupId>org.example</groupId>
    <artifactId>log4j2demo</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>

運行結(jié)果橄唬,全部使用了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

引入依賴

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>log4jdemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <name>log4jdemo</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <!--日志 start-->
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

        <!--將基礎(chǔ)庫升級到slf4j赋焕,使用slf4j的api打印日志-->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.7.29</version>
        </dependency>

        <!--日志end-->
    </dependencies>
</project>

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

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>org.example</groupId>
    <artifactId>log4j2demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.12.1</version>
        </dependency>
        <!--將基礎(chǔ)庫升級到slf4j,使用slf4j的api打印日志-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.9.0</version>
        </dependency>
    </dependencies>
</project>

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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末轧坎,一起剝皮案震驚了整個濱河市宏邮,隨后出現(xiàn)的幾起案子泽示,更是在濱河造成了極大的恐慌缸血,老刑警劉巖,帶你破解...
    沈念sama閱讀 206,482評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件械筛,死亡現(xiàn)場離奇詭異捎泻,居然都是意外死亡,警方通過查閱死者的電腦和手機埋哟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,377評論 2 382
  • 文/潘曉璐 我一進店門笆豁,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人赤赊,你說我怎么就攤上這事闯狱。” “怎么了抛计?”我有些...
    開封第一講書人閱讀 152,762評論 0 342
  • 文/不壞的土叔 我叫張陵哄孤,是天一觀的道長。 經(jīng)常有香客問我吹截,道長瘦陈,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,273評論 1 279
  • 正文 為了忘掉前任波俄,我火速辦了婚禮晨逝,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘懦铺。我一直安慰自己捉貌,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 64,289評論 5 373
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著趁窃,像睡著了一般苍匆。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上棚菊,一...
    開封第一講書人閱讀 49,046評論 1 285
  • 那天浸踩,我揣著相機與錄音,去河邊找鬼统求。 笑死检碗,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的码邻。 我是一名探鬼主播折剃,決...
    沈念sama閱讀 38,351評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼像屋!你這毒婦竟也來了怕犁?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 36,988評論 0 259
  • 序言:老撾萬榮一對情侶失蹤己莺,失蹤者是張志新(化名)和其女友劉穎奏甫,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體凌受,經(jīng)...
    沈念sama閱讀 43,476評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡阵子,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,948評論 2 324
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了胜蛉。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片挠进。...
    茶點故事閱讀 38,064評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖誊册,靈堂內(nèi)的尸體忽然破棺而出领突,到底是詐尸還是另有隱情,我是刑警寧澤案怯,帶...
    沈念sama閱讀 33,712評論 4 323
  • 正文 年R本政府宣布君旦,位于F島的核電站,受9級特大地震影響殴泰,放射性物質(zhì)發(fā)生泄漏于宙。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 39,261評論 3 307
  • 文/蒙蒙 一悍汛、第九天 我趴在偏房一處隱蔽的房頂上張望捞魁。 院中可真熱鬧,春花似錦离咐、人聲如沸谱俭。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,264評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽昆著。三九已至县貌,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間凑懂,已是汗流浹背煤痕。 一陣腳步聲響...
    開封第一講書人閱讀 31,486評論 1 262
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留接谨,地道東北人摆碉。 一個月前我還...
    沈念sama閱讀 45,511評論 2 354
  • 正文 我出身青樓,卻偏偏與公主長得像脓豪,于是被迫代替她去往敵國和親巷帝。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 42,802評論 2 345

推薦閱讀更多精彩內(nèi)容