Java Log4j 2. 如何获取log4j 的调试信息?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19574413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:27:09  来源:igfitidea点击:

Log4j 2. How get log4j's debug messages?

javalogginglog4jlog4j2

提问by Kirill Popov

As far as i understand log4j can handle system property -Dlog4j.debug. If you run your app with it you will get log4j's debug output.

据我了解,log4j 可以处理系统属性 -Dlog4j.debug。如果您使用它运行您的应用程序,您将获得 log4j 的调试输出。

Example: java -Dlog4j.debug -jar test.jar

示例:java -Dlog4j.debug -jar test.jar

Is there something similar for log4j 2?

log4j 2 有类似的东西吗?

采纳答案by Remko Popma

Update January 2018:

2018 年 1 月更新:

From Log4j 2.10, this is easy: just run your program with system property log4j2.debug(no value needed; an empty string is fine).

从 Log4j 2.10 开始,这很简单:只需使用系统属性运行您的程序log4j2.debug(不需要值;空字符串就可以)。



The current (log4j-2.1) documentation on the status logger is a bit confusing. Basically:

关于状态记录器的当前 (log4j-2.1) 文档有点混乱。基本上:

  • Until a configuration is found, status logger level can be controlled with system property org.apache.logging.log4j.simplelog.StatusLogger.level.
  • After a configuration is found, status logger level can be controlled in the configuration file with the "status" attribute, for example: <Configuration status="trace">.
  • 在找到配置之前,可以使用系统属性控制状态记录器级别org.apache.logging.log4j.simplelog.StatusLogger.level
  • 找到配置后,可以在配置文件中使用“status”属性控制状态记录器级别,例如:<Configuration status="trace">.

UPDATE: the documentationwas improved in log4j-2.2.

更新:在 log4j-2.2 中改进了文档

回答by dimo414

I've had a frustrating amount of difficulty getting Log4J2 up and running, and printing the StatusLogger is no exception. In theory you can set it in the config file with the statusfield, however I've not been able to make that work thusfar.

我在启动和运行 Log4J2 时遇到了令人沮丧的困难,打印 StatusLogger 也不例外。从理论上讲,您可以在带有status字段的配置文件中设置它,但是到目前为止我还无法完成这项工作。

You can run the following at the beginning of your main method however:

但是,您可以在 main 方法的开头运行以下内容:

StatusConsoleListener listener = new StatusConsoleListener(Level.ALL);
StatusLogger.getLogger().registerListener(listener);
LogManager.getLogger(LogManager.ROOT_LOGGER_NAME); // initialize logger

Note your main()class cannot have any static Loggers, or they'll be initialized before this is called, meaning the loading status messages won't print.

请注意,您的main()类不能有任何静态记录器,否则它们将在调用之前被初始化,这意味着不会打印加载状态消息。

回答by Robert Hunt

It can be confusing, the nearest equivilent of the Log4J 1.x command line argument -Dlog4j.debugis -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=tracewhich sets the Log4J 2.x "status logger" level to trace and provides detailed output about the logging configuration.

可能会令人困惑,Log4J 1.x 命令行参数的最近等效项-Dlog4j.debug-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=trace将 Log4J 2.x “状态记录器”级别设置为跟踪并提供有关日志记录配置的详细输出。

Log4J 1.x allows you to manually specify the location of the configuration file on the command line using -Dlog4j.configuration=file:///var/lib/tomcat7/log4j.xmlwhere the configuration file is located at /var/lib/tomcat7/log4j.xml. In Log4J 2.x there is a subtle difference in the argument -Dlog4j.configurationFile=file:///var/lib/tomcat7/log4j.xml, 'configurationFile' rather than 'configuration'.

Log4J 1.x 允许您使用-Dlog4j.configuration=file:///var/lib/tomcat7/log4j.xml配置文件所在的位置在命令行上手动指定配置文件的位置/var/lib/tomcat7/log4j.xml。在 Log4J 2.x 中,参数-Dlog4j.configurationFile=file:///var/lib/tomcat7/log4j.xml“配置文件”而不是“配置”有细微差别。

Obviously you need to ensure that your configuration file is suitible for the version of Log4J being used, the XML structure differs between 1.x and 2.x.

显然您需要确保您的配置文件适合所使用的 Log4J 版本,XML 结构在 1.x 和 2.x 之间有所不同。

回答by BurningPapaya

In case someone needs to set DEBUGlevel programmatically

如果有人需要以DEBUG编程方式设置级别

// for your custom logger
Configurator.setLevel("com.name.of.logger", Level.DEBUG);
// for root logger
Configurator.setRootLevel(Level.DEBUG);

OR without imports

或没有进口

org.apache.logging.log4j.core.config.Configurator.setLevel(
    "com.name.of.logger", 
    org.apache.logging.log4j.Level.DEBUG
);