gpt4 book ai didi

java - log4j V2 中 log4j.properties 的内容并格式化记录器输出(例如添加时间戳)

转载 作者:行者123 更新时间:2023-11-30 02:47:03 24 4
gpt4 key购买 nike

我正在做一个Java项目,并且我已经将Log4j API版本2集成到我的程序中(显然这是第一次,我不知道它是如何工作的,因此如果我的问题看起来很简单别把责任归咎于我)。我的log4j.properties的内容如下:

log4j.rootLogger=DEBUG,SAWAN
log4j.appender.SAWAN=org.apache.log4j.ConsoleAppender
log4j.appender.SAWAN.layout=org.apache.log4j.SimpleLayout

然后我将 log4j 库导入到我的类中,出于调试目的,我编写了以下内容并显示了输出

    //My Code
PropertyConfigurator.configure("log4j.properties");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");


//Output
DEBUG - Sample debug message
INFO - Sample info message
WARN - Sample warn message
ERROR - Sample error message
FATAL - Sample fatal message

这意味着 log4j 工作正常。我多么想更改输出的格式并向其添加时间戳。基于我对本网站提出的其他问题的研究并引用 https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html我知道我需要使用类似的东西:

[%t] %-5p %c %x - %m%n

但是一旦我将其添加到我的 log4j.properties 中,我收到了一个错误。我的问题是我应该在哪里指定日志输出的输出格式。

以下是我导入的库:

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.PatternLayout;

值得一提的是,我尝试将以下内容添加到我的 log4j.properties 中: log4j.appender.ConsoleAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n

但是我不断收到错误,表明我的 log4j.properties 有问题,但当我删除它时情况并非如此,它会正常工作:)

最佳答案

您需要在类路径上有一个名为 log4j2.properties 的文件才能自动获取。

来自 Log4j2 的文档:

Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for XML.

Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.

  1. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
  2. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
  3. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
  4. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  5. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
  6. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
  7. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  8. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  9. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

您可以在此处找到有关 log4j2 配置的所有文档:https://logging.apache.org/log4j/2.x/manual/configuration.html

感谢D.B.指出正确答案。

关于java - log4j V2 中 log4j.properties 的内容并格式化记录器输出(例如添加时间戳),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39911106/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com