提交 16d8698f 编写于 作者: X xuml

feat: 补充Log4j2的binding ,并完善相应文档。

上级 45214164
......@@ -5,78 +5,104 @@
测试结果:
```
2023-10-27 10:37:09 DEBUG (com.feiyizhan.LogJclTests:21) - LogJclTests 这是debug日志...
2023-10-27 10:37:09 INFO (com.feiyizhan.LogJclTests:23) - LogJclTests 这是info日志...
2023-10-27 10:37:09 WARN (com.feiyizhan.LogJclTests:24) - LogJclTests 这是warn日志...
2023-10-27 10:37:09 ERROR (com.feiyizhan.LogJclTests:25) - LogJclTests 这是error日志...
2023-10-27 10:37:09 INFO (com.feiyizhan.LogJclTests:26) - LogJclTests public class org.apache.commons.logging.impl.Log4JLogger
十月 27, 2023 10:37:09 上午 com.feiyizhan.LogJulTests printLog
2023-10-27 13:34:48 DEBUG (com.feiyizhan.LogJclTests:21) - LogJclTests 这是debug日志...
2023-10-27 13:34:48 INFO (com.feiyizhan.LogJclTests:23) - LogJclTests 这是info日志...
2023-10-27 13:34:48 WARN (com.feiyizhan.LogJclTests:24) - LogJclTests 这是warn日志...
2023-10-27 13:34:48 ERROR (com.feiyizhan.LogJclTests:25) - LogJclTests 这是error日志...
2023-10-27 13:34:48 INFO (com.feiyizhan.LogJclTests:26) - LogJclTests public class org.apache.commons.logging.impl.Log4JLogger
十月 27, 2023 1:34:48 下午 com.feiyizhan.LogJulTests printLog
信息: LogJulTests 这是info日志...
十月 27, 2023 10:37:09 上午 com.feiyizhan.LogJulTests printLog
十月 27, 2023 1:34:48 下午 com.feiyizhan.LogJulTests printLog
信息: LogJulTests public class java.util.logging.Logger
2023-10-27 10:37:09 DEBUG (com.feiyizhan.LogLog4jTests:21) - LogLog4jTests 这是debug日志...
2023-10-27 10:37:09 INFO (com.feiyizhan.LogLog4jTests:23) - LogLog4jTests 这是info日志...
2023-10-27 10:37:09 WARN (com.feiyizhan.LogLog4jTests:24) - LogLog4jTests 这是warn日志...
2023-10-27 10:37:09 ERROR (com.feiyizhan.LogLog4jTests:25) - LogLog4jTests 这是error日志...
2023-10-27 10:37:09 INFO (com.feiyizhan.LogLog4jTests:26) - LogLog4jTests public class org.apache.log4j.Logger
2023-10-27 13:34:48 DEBUG (com.feiyizhan.LogLog4jTests:21) - LogLog4jTests 这是debug日志...
2023-10-27 13:34:48 INFO (com.feiyizhan.LogLog4jTests:23) - LogLog4jTests 这是info日志...
2023-10-27 13:34:48 WARN (com.feiyizhan.LogLog4jTests:24) - LogLog4jTests 这是warn日志...
2023-10-27 13:34:48 ERROR (com.feiyizhan.LogLog4jTests:25) - LogLog4jTests 这是error日志...
2023-10-27 13:34:48 INFO (com.feiyizhan.LogLog4jTests:26) - LogLog4jTests public class org.apache.log4j.Logger
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repository/org/slf4j/slf4j-simple/1.7.36/slf4j-simple-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repository/org/slf4j/slf4j-jdk14/1.7.36/slf4j-jdk14-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repository/org/slf4j/slf4j-jcl/1.7.36/slf4j-jcl-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repository/org/slf4j/slf4j-reload4j/1.7.36/slf4j-reload4j-1.7.36.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.21.1/log4j-slf4j-impl-2.21.1.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 [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
10:37:09.295 [main] INFO com.feiyizhan.LogBackTests - LogBackTests 这是info日志...
10:37:09.298 [main] WARN com.feiyizhan.LogBackTests - LogBackTests 这是warn日志...
10:37:09.298 [main] ERROR com.feiyizhan.LogBackTests - LogBackTests 这是error日志...
10:37:09.298 [main] INFO com.feiyizhan.LogBackTests - LogBackTests 这是info日志...test
10:37:09.300 [main] INFO com.feiyizhan.LogBackTests - LogBackTests public final class ch.qos.logback.classic.Logger
10:37:09.301 [main] INFO com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests 这是info日志...
10:37:09.302 [main] WARN com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests 这是warn日志...
10:37:09.302 [main] ERROR com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests 这是error日志...
10:37:09.302 [main] INFO com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests 这是info日志...test
10:37:09.302 [main] INFO com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests public final class ch.qos.logback.classic.Logger
13:34:48.953 [main] INFO com.feiyizhan.LogBackTests - LogBackTests 这是info日志...
13:34:48.958 [main] WARN com.feiyizhan.LogBackTests - LogBackTests 这是warn日志...
13:34:48.958 [main] ERROR com.feiyizhan.LogBackTests - LogBackTests 这是error日志...
13:34:48.958 [main] INFO com.feiyizhan.LogBackTests - LogBackTests 这是info日志...test
13:34:48.962 [main] INFO com.feiyizhan.LogBackTests - LogBackTests public final class ch.qos.logback.classic.Logger
13:34:48.962 [main] INFO com.feiyizhan.LogLog4j2Tests - 这是info日志...
13:34:48.962 [main] WARN com.feiyizhan.LogLog4j2Tests - 这是warn日志...
13:34:48.962 [main] ERROR com.feiyizhan.LogLog4j2Tests - 这是error日志...
13:34:48.963 [main] INFO com.feiyizhan.LogLog4j2Tests - 这是info日志...test
13:34:48.963 [main] INFO com.feiyizhan.LogLog4j2Tests - public final class ch.qos.logback.classic.Logger
13:34:48.963 [main] INFO com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests 这是info日志...
13:34:48.964 [main] WARN com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests 这是warn日志...
13:34:48.964 [main] ERROR com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests 这是error日志...
13:34:48.964 [main] INFO com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests 这是info日志...test
13:34:48.964 [main] INFO com.feiyizhan.LogSlf4jSimpleTests - LogSlf4jSimpleTests public final class ch.qos.logback.classic.Logger
```
最终使用的是的加载的第一个`StaticLoggerBinder``logback` 来输出日志。
slfj的常用的bind包有
```
<!--jul的bind包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--jcl的bind包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>${slf4j.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<!--log4j 的bind包,已经弃用-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- Log4j binding SLF4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
slfj的常用的binding包有
```xml
<!-- SLF4j binding logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<!--SLF4j simple binding-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!--SLF4j binding jul -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- SLF4j binding jcl -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
<version>${slf4j.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- SLF4j binding Log4j (已弃用)-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- SLF4j binding Log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- SLF4j binding Log4j2-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
```
其中`slf4j-log4j12` 已经被`slf4j-reload4j` 替代,引入`slf4j-log4j12` 会被自动替换成`slf4j-reload4j` ,因此建议直接使用`slf4j-reload4j`
......
......@@ -17,6 +17,7 @@
<properties>
<java.version>1.8</java.version>
<slf4j.version>1.7.36</slf4j.version>
<log4j2.version>2.21.1</log4j2.version>
</properties>
<dependencies>
<dependency>
......@@ -67,11 +68,13 @@
<!-- <version>1.2.17</version>-->
<!-- </dependency>-->
<!--引入Logback-->
<!--Log4j2相关的依赖包-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j2.version}</version>
</dependency>
<!--添加jcl 依赖-->
......@@ -81,7 +84,14 @@
<version>1.2</version>
</dependency>
<!--添加Slfj simple依赖-->
<!-- SLF4j binding logback-->
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.11</version>
</dependency>
<!--SLF4j simple binding-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
......@@ -89,14 +99,14 @@
</dependency>
<!--添加slf4j binding依赖-->
<!--SLF4j binding jul -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- JCL binding SLF4j -->
<!-- SLF4j binding jcl -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jcl</artifactId>
......@@ -109,13 +119,20 @@
</exclusions>
</dependency>
<!-- Log4j binding SLF4j-->
<!-- SLF4j binding Log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-reload4j</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- SLF4j binding Log4j2-->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j2.version}</version>
</dependency>
</dependencies>
<build>
......
#设置日志级别为info
logging.level.root = info
#logging.config=classpath:logging.properties
#logging.pattern.console=[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%X{requestId}] [%t] %-5level: [%c{1.}:%L] - %m%n
logging.pattern.console=[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%X{requestId}] [%t] %-5level: [%c{1.}:%L] - %m%n
......@@ -16,6 +16,7 @@ public class LogSlf4jMultiBindingTests {
LogJulTests.printLog();
LogLog4jTests.printLog();
LogBackTests.printLog();
LogLog4j2Tests.printLog();
LogSlf4jSimpleTests.printLog();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册