簡體   English   中英

log4j:無法將日志記錄設置為 DEBUG

[英]log4j: cannot set logging to DEBUG

我在使用 log4j 時遇到問題 --- 我正在嘗試使用 log4j.properties 文件將日志記錄級別設置為 DEBUG。 我知道這部分有效,因為另一個組件 .netty ) 有效,但當它到達我時,日志記錄設置為 ERROR 及以上。

這是我的 log4j.properties 文件:

#Define root logger options
log4j.rootLogger=DEBUG, console

log4j.logger.com.ltsllc.miranda=DEBUG

#Define console appender

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=%-5p %c{1} - %m%n

這是檢查調試級別的代碼:

    if (l.isDebugEnabled()) {
        l.debug("DEBUG is enabled");
    } else {
        l.error ("Debug is disabled");
        l.error ("level is: " + l.getLevel());
    }

這是 output:

DEBUG ResourceLeakDetector - -Dio.netty.leakDetection.level: simple
DEBUG ResourceLeakDetector - -Dio.netty.leakDetection.targetRecords: 4
DEBUG ResourceLeakDetectorFactory - Loaded default ResourceLeakDetector: io.netty.util.ResourceLeakDetector@44b3606b
22:43:46.696 \[main\] ERROR com.ltsllc.miranda.Miranda - Debug is disabled
22:43:46.700 \[main\] ERROR com.ltsllc.miranda.Miranda - level is: ERROR
DEBUG DefaultChannelId - -Dio.netty.processId: 30572 (auto-detected)
DEBUG NetUtil - -Djava.net.preferIPv4Stack: false

我期待這樣的事情

DEGUG com.ltsllc.miranda.Miranda - DEBUG is enabled

從您提供的詳細信息來看,問題可能源於各種來源:

  1. 驗證記錄器初始化:確保為“com.ltsllc.miranda”package 正確初始化了“l”記錄器。您可能正在使用用於不同 package 的記錄器,導致它無法獲取 DEBUG 設置。

    這是一個例子:

     import org.apache.log4j.Logger; public class Miranda { private static final Logger l = Logger.getLogger(Miranda.class); }
  2. 確認配置文件的位置:檢查 log4j.properties 文件的位置是否正確。 它需要在類路徑上。 通常對於 Maven 項目,您會將其放在src/main/resources目錄中。

  3. 檢查多個配置:如果您有多個 log4j 配置文件(例如 log4j.xml 和 log4j.properties),則 Log4j 可能是從錯誤的文件中提取的。 最好只有一個配置文件,或者具體說明使用哪一個。

  4. 可能覆蓋 log4j 配置:某些庫或框架可能會無意中覆蓋您的 log4j 配置。 例如,Spring Boot 默認情況下會自動配置日志記錄。 值得考慮的是,您的情況是否會發生類似的事情。

  5. 考慮初始化的順序:您的日志系統可能比您的某些代碼初始化得晚。 確保在您的應用程序生命周期中盡早初始化它。

檢查這些潛在原因應該可以幫助您識別和解決問題。

當未提供配置時,Log4j2 參考實現 ( log4j-core ) 使用%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n模式和ERROR級別。 這就是您在標准 output 上觀察到的。

日志記錄 API 和后端可能綁定如下:

  • Netty 的內部日志記錄 API(參見InternalLoggerFactory )綁定到 SLF4J( slf4j-api ),它使用 Log4j 1.x 或 Reload4j 作為后端。 這個后端就是你配置的那個。
  • 您在代碼中使用的 Log4j2 API(參見API 分離)綁定到您未配置的Log4j2 參考實現。 我猜測您的代碼中l的完全限定 class 名稱是org.apache.logging.log4j.Logger

我假設您不想使用 Log4j 1.x 作為后端(它在 8 年前被宣布停產)所以您需要:

  1. 通過添加包含以下內容的log4j2.xml文件來配置log4j-core
     <Configuration> <Appenders> <Console name="console"> <PatternLayout pattern="%-5p %c{1} - %m%n"/> </Console> </Appenders> <Loggers> <Root level="DEBUG"> <AppenderRef ref="console"/> </Root> </Loggers> </Configuration>
  2. 通過以下方式修復您的依賴項:
    • 從類路徑中刪除log4j:log4jch.qos.reload4j:reload4j工件。 這還包括 SLF4J 綁定org.slf4j:slf4j-log4j12org.slf4j:slf4j-reload4j
    • 如果 SLF4J ( slf4j-api ) 不存在,Netty 將綁定到 Log4j2 API,但其他庫可能直接使用 SLF4J。 如果您使用的是 Maven,則需要這 3 個工件:
       <dependencyManagement> <,-- Add the BOM. so you don't need to specify version --> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-bom</artifactId> <version>2.20.0</version> <scope>import</scope> <type>pom</type> </dependency> </dependencies> </dependencyManagement> <dependencies> <.-- Used in your code --> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> </dependency> <.-- Only necessary at runtime --> <dependency> <groupId>org:apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <scope>runtime</scope> </dependency> <.-- ~ If some of your libraries use SLF4J API directly ~ bind them to `log4j-api`. --> <dependency> <groupId>org.apache.logging.log4j</groupId> <!-- Replace with log4j-slf4j2-impl if SLF4J 2.x is used --> <artifactId>log4j-slf4j-impl</artifactId> <scope>runtime</scope> </dependency> </dependencies>
      如果您的庫使用其他日志 API(例如 Jakarta Apache Commons Logging、 java.util.logging 、JBoss Logging),您還需要將它們綁定到log4j-api

編輯:我建議不要使用 Log4j 2.x 的屬性配置格式,因為以平面格式存儲樹狀結構非常冗長。 此外,當 Log4j 1.x 發布時,JRE 沒有 XML 解析器,現在有了。

上面的 XML 配置可以轉換為屬性:

appender.0.type = Console
appender.0.name = console
appender.0.0.type = PatternLayout
appender.0.0.pattern = %-5p %c{1} - %m%n

logger.rootLogger.level = DEBUG
logger.rootLogger.appenderRef.0.ref = console
##
# The `rootLogger` config can be abbreviated in recent versions to
#   logger.rootLogger = DEBUG, console

我在有關如何以編程方式設置日志記錄級別的問題中找到了答案:

LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
LoggerConfig loggerConfig = config.getLoggerConfig(LogManager.ROOT_LOGGER_NAME); 
loggerConfig.setLevel(level);
ctx.updateLoggers();  // This causes all Loggers to refetch information from their LoggerConfig.

這是這個問題的答案

暫無
暫無

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

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