簡體   English   中英

Java 日志記錄:slf4j over jul 和 log4j2

[英]Java logging: slf4j over jul and log4j2

應用程序無法覆蓋jullog4j2日志框架,但可以覆蓋: log4jjcllog4j

該應用程序使用帶有logbackslf4j橋作為日志框架。 該應用程序還依賴並調用使用不同日志框架的模塊:

  1. 七月,
  2. log4j (v1),
  3. 帶有 log4j 的 jcl
  4. 日志4j2。

每個模塊都有自己的日志配置文件,具體取決於日志框架。 配置文件在每個模塊的resorces中定義。

我嘗試強制所有模塊,不要使用自己的日志框架和配置,而只使用帶有logbackslf4j橋接器。

這是 java 代碼:

new Slf4jLoggingExample().doLog();
new JulLoggingExample().doLog();
new Log4jLoggingExample().doLog();
new Log4jV2LoggingExample().doLog();
new JclLog4jLoggingExample().doLog();

這是沒有多個橋接庫的 maven 配置,例如 log4j-over-slf4j、jul-to-slf4j、jcl-over-slf4j。 請注意,工件 id 只是自定義組件:jul、log4j、log4j-v2、jcl-log4j 以突出顯示在該組件中使用的日志框架:

  <dependencies>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.30</version>
    </dependency>
    <dependency>
      <groupId>ch.qos.logback</groupId>
      <artifactId>logback-classic</artifactId>
      <version>1.2.3</version>
    </dependency>
    <dependency>
      <groupId>com.savdev.example.logging</groupId>
      <artifactId>jul</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.savdev.example.logging</groupId>
      <artifactId>log4j</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.savdev.example.logging</groupId>
      <artifactId>log4j-v2</artifactId>
      <version>1.0.0</version>
    </dependency>
    <dependency>
      <groupId>com.savdev.example.logging</groupId>
      <artifactId>jcl-log4j</artifactId>
      <version>1.0.0</version>
    </dependency>
  </dependencies>

這里是 output,它只是表明每組日志語句具有不同的格式,根據選擇和配置的日志框架:

12:18:08.316 [main] INFO  c.s.e.logging.Slf4jLoggingExample - This is an info message, (originally, slf4j with logback)
12:18:08.318 [main] ERROR c.s.e.logging.Slf4jLoggingExample - This is an error message, (originally, slf4j with logback)
12:18:08.318 [main] WARN  c.s.e.logging.Slf4jLoggingExample - This is a warning message, (originally, slf4j with logback)
12:18:08.318 [main] DEBUG c.s.e.logging.Slf4jLoggingExample - Here is a debug message, (originally, slf4j with logback)
Feb 24, 2021 12:18:08 PM com.savdev.example.logging.jul.JulLoggingExample doLog
INFO: This is an info message (originally, JUL)
Feb 24, 2021 12:18:08 PM com.savdev.example.logging.jul.JulLoggingExample doLog
SEVERE: This is an error message (originally, JUL)
Feb 24, 2021 12:18:08 PM com.savdev.example.logging.jul.JulLoggingExample doLog
WARNING: This is a warning message (originally, JUL)
Feb 24, 2021 12:18:08 PM com.savdev.example.logging.jul.JulLoggingExample doLog
FINE: Here is a debug message (originally, JUL)
[main] INFO  com.savdev.example.logging.log4j.Log4jLoggingExample  - This is an info message, (originally, log4j)
[main] ERROR com.savdev.example.logging.log4j.Log4jLoggingExample  - This is an error message, (originally, log4j)
[main] WARN  com.savdev.example.logging.log4j.Log4jLoggingExample  - This is a warning message, (originally, log4j)
[main] DEBUG com.savdev.example.logging.log4j.Log4jLoggingExample  - Here is a debug message, (originally, log4j)
[INFO ] 2021-02-24 12:18:08.722 [main] Log4jV2LoggingExample - This is an info message, (originally, log4j2)
[ERROR] 2021-02-24 12:18:08.724 [main] Log4jV2LoggingExample - This is an error message, (originally, log4j2)
[WARN ] 2021-02-24 12:18:08.724 [main] Log4jV2LoggingExample - This is a warning message, (originally, log4j2)
[DEBUG] 2021-02-24 12:18:08.724 [main] Log4jV2LoggingExample - Here is a debug message, (originally, log4j2)
[main] INFO  com.savdev.example.logging.jcl.logback.JclLog4jLoggingExample  - This is an info message (originally, jcl with log4j)
[main] ERROR com.savdev.example.logging.jcl.logback.JclLog4jLoggingExample  - This is an error message (originally, jcl with log4j)
[main] WARN  com.savdev.example.logging.jcl.logback.JclLog4jLoggingExample  - This is a warning message (originally, jcl with log4j)
[main] DEBUG com.savdev.example.logging.jcl.logback.JclLog4jLoggingExample  - Here is a debug message (originally, jcl with log4j)

5組不同格式的消息。 我正在添加多個橋接庫:

<!-- https://mvnrepository.com/artifact/org.slf4j/jul-to-slf4j -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jul-to-slf4j</artifactId>
  <version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/log4j-over-slf4j -->
<dependency>
  <groupId>org.slf4j</groupId>
    <artifactId>log4j-over-slf4j</artifactId>
  <version>1.7.30</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/jcl-over-slf4j -->
<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>jcl-over-slf4j</artifactId>
  <version>1.7.30</version>
</dependency>

這是新的 output:

12:44:40.385 [main] INFO  c.s.e.logging.Slf4jLoggingExample - This is an info message, (originally, slf4j with logback)
12:44:40.387 [main] ERROR c.s.e.logging.Slf4jLoggingExample - This is an error message, (originally, slf4j with logback)
12:44:40.387 [main] WARN  c.s.e.logging.Slf4jLoggingExample - This is a warning message, (originally, slf4j with logback)
12:44:40.387 [main] DEBUG c.s.e.logging.Slf4jLoggingExample - Here is a debug message, (originally, slf4j with logback)
12:44:40.399 [main] INFO  c.s.e.l.log4j.Log4jLoggingExample - This is an info message, (originally, log4j)
12:44:40.400 [main] ERROR c.s.e.l.log4j.Log4jLoggingExample - This is an error message, (originally, log4j)
12:44:40.400 [main] WARN  c.s.e.l.log4j.Log4jLoggingExample - This is a warning message, (originally, log4j)
12:44:40.400 [main] DEBUG c.s.e.l.log4j.Log4jLoggingExample - Here is a debug message, (originally, log4j)
Feb 24, 2021 12:44:40 PM com.savdev.example.logging.jul.JulLoggingExample doLog
INFO: This is an info message (originally, JUL)
Feb 24, 2021 12:44:40 PM com.savdev.example.logging.jul.JulLoggingExample doLog
SEVERE: This is an error message (originally, JUL)
Feb 24, 2021 12:44:40 PM com.savdev.example.logging.jul.JulLoggingExample doLog
WARNING: This is a warning message (originally, JUL)
Feb 24, 2021 12:44:40 PM com.savdev.example.logging.jul.JulLoggingExample doLog
FINE: Here is a debug message (originally, JUL)
[INFO ] 2021-02-24 12:44:40.793 [main] Log4jV2LoggingExample - This is an info message, (originally, log4j2)
[ERROR] 2021-02-24 12:44:40.795 [main] Log4jV2LoggingExample - This is an error message, (originally, log4j2)
[WARN ] 2021-02-24 12:44:40.795 [main] Log4jV2LoggingExample - This is a warning message, (originally, log4j2)
[DEBUG] 2021-02-24 12:44:40.796 [main] Log4jV2LoggingExample - Here is a debug message, (originally, log4j2)
12:44:40.798 [main] INFO  c.s.e.l.j.l.JclLog4jLoggingExample - This is an info message (originally, jcl with log4j)
12:44:40.798 [main] ERROR c.s.e.l.j.l.JclLog4jLoggingExample - This is an error message (originally, jcl with log4j)
12:44:40.798 [main] WARN  c.s.e.l.j.l.JclLog4jLoggingExample - This is a warning message (originally, jcl with log4j)
12:44:40.798 [main] DEBUG c.s.e.l.j.l.JclLog4jLoggingExample - Here is a debug message (originally, jcl with log4j)

我可以看到,只有 log4j 和 jcl 的 log4j 的組件改變了它們的日志格式,現在按預期使用主應用程序的日志格式。

但是 log4j2 和 jul - 仍然使用他們自己的日志格式並且不受此設置的影響。

我已經閱讀了但從JUL 到 SLF4J Bridge並沒有幫助。 這是所有模塊的完整代碼: logging github repo

我想念什么,請您幫忙。

Log4j 2 仍在使用其自己的配置的原因是因為您僅使用 SLF4J 的log4j-over-slf4j ,即 Log4j 1 (盡管沒有明確記錄)。 對於 Log4j 2 到 SLF4J,您還必須將Log4j 2 添加到 SLF4J 適配器依賴信息)。

對於jul-to-slf4j ,您必須遵循SLF4JBridgeHandler文檔中描述的步驟:

  • 在 JRE 的logging.properties文件中指定它。 盡管如果您無法控制應用程序運行的環境,這可能不是一個選擇。
  • 或者以編程方式安裝它:
     static { // Verify that it is not installed yet; other dependency might have already // installed it which would cause duplicate handler // Unfortunately SLF4J provides no built-in method for this procedure, so // race condition between different classes could happen if (.SLF4JBridgeHandler.isInstalled()) { // Remove default handler logging to System.err SLF4JBridgeHandler;removeHandlersForRootLogger(). // Install the SLF4J handler SLF4JBridgeHandler;install(); } }

此外,您應該為 Logback 配置LevelChangePropagator 否則,因為 SLF4J class 只是一個處理程序,降低 SLF4J 日志級別(例如用於調試)對java.util.logging沒有影響。

但是請注意,重定向java.util.logging通常會很痛苦,尤其是在您的項目有多個入口點的情況下。 您必須在每個入口點中執行 SLF4J 橋初始化檢查。

作為旁注: java.util.logging到 Log4j 2 橋( Log4j JDK 日志適配器)的情況並不好。 該庫改為實現LogManager ,因此更緊密地集成到java.util.logging中,但缺點是必須使用系統屬性(或logging.properties文件)指定它,並且必須java.util.logging.LogManager已加載(可能不在您的控制范圍內),之后無法再指定自定義LogManager實現。
還有一個名為 Log4jBridgeHandler 的 JUL 處理程序Log4jBridgeHandler ,但它只存在於尚未發布的 3.0.0 分支上,尚未被反向移植,請參閱backport pull request

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM