如果不了解標(biāo)題提到的幾個日志框架帅涂,建議了解一下log4j/log4j2/logback簡單使用說明退敦,本節(jié)中pom依賴前面的內(nèi)容助析;
代碼地址
log4jdemo
log4j2demo
logbackdemo
logdemo
背景說明
java項目一般會選擇一種日志框架缠导,但很難保證當(dāng)前項目依賴的三方組件會選用和當(dāng)前項目相同的日志框架朴恳,比如當(dāng)前項目使用了logback窜骄,項目依賴了ES蛾魄,ES使用了log4j2日志框架,這時日志打印可能會有問題逛拱,甚至不打印日志敌厘,影響問題排查......
情景一
此時:
1.不在此項目(project)中指定日志屬性配置文件,原項目(project A,project B,project C)中各自使用原生日志框架類打印日志朽合,則用各自的日志配置文件打印日志俱两。
2.在此項目中指定日志屬性配置文件,原項目中的類則忽略原項目中的日志屬性配置文件曹步,用此項目中的日志屬性配置文件宪彩。
情景二
原項目(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
情景三
或者
如果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