您需要多久设置一次 log4j2 的配置文件?
How often do you need to set the configuration file for log4j2?
我在一个包中有多个 classes,在我的项目中有多个包。
对于那些继承的文件,在父 class 中是
{ // File location for log4j2.xml
System.setProperty("log4j.configurationFile",
"file:\\" + System.getProperty("user.dir") + "\Properties\log4j2.xml");
}
然而,我看到了 ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
所以,显然,一次是不够的。
那么我需要设置 log4j.configurationFile
的系统 属性 的一般规则是什么?
- 每个包裹一次?
- 每 class 一次?
- 每个parent/baseclass,不管包?
- 还是别的?
多个 class 和包的最佳做法是什么?
Log4j 2 在初始化时自行配置,即每当在 JVM 实例中首次创建 Logger 实例时,大约......应用程序中声明 Logger 实例的第一个 class 会导致 Log4j 2 初始化本身。所以,这个问题的答案...
How often do you need to set the configuration file for log4j2?
...是一次。
您的 OP 中报告的问题:No log4j2 configuration file found. Using default configuration
是由于 Log4j 无法找到您试图指向的配置文件造成的。此问题 不是 引起的 'setting the configuration file' 不够频繁。
根据the docs Log4j 将按如下方式查找其配置源:
- 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.
- If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
- If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
- If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
- If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
- If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
- If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the
classpath.
- If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
- If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
- If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
如果您尝试设置 log4j.configurationFile
成功,那么选项 1
将被启用,但该尝试不成功,我们知道这一点,因为您引用的错误消息告诉我们 Log4j 正在失败选项 10
.
我建议 运行 您的应用程序带有 -Dlog4j.configurationFile=/path/to/log4j2.xml 或确保 log4j2.xml
在您的应用程序的 class路径。用 System.setProperty(...)
调用乱丢代码的想法感觉像是一种反模式,并且可能非常脆弱,因为在 System.setProperty(...)
调用 之前 实例化的任何静态记录器将导致 Log4j 在知道您的配置文件位置之前自行初始化。
我在一个包中有多个 classes,在我的项目中有多个包。 对于那些继承的文件,在父 class 中是
{ // File location for log4j2.xml
System.setProperty("log4j.configurationFile",
"file:\\" + System.getProperty("user.dir") + "\Properties\log4j2.xml");
}
然而,我看到了 ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
所以,显然,一次是不够的。
那么我需要设置 log4j.configurationFile
的系统 属性 的一般规则是什么?
- 每个包裹一次?
- 每 class 一次?
- 每个parent/baseclass,不管包?
- 还是别的?
多个 class 和包的最佳做法是什么?
Log4j 2 在初始化时自行配置,即每当在 JVM 实例中首次创建 Logger 实例时,大约......应用程序中声明 Logger 实例的第一个 class 会导致 Log4j 2 初始化本身。所以,这个问题的答案...
How often do you need to set the configuration file for log4j2?
...是一次。
您的 OP 中报告的问题:No log4j2 configuration file found. Using default configuration
是由于 Log4j 无法找到您试图指向的配置文件造成的。此问题 不是 引起的 'setting the configuration file' 不够频繁。
根据the docs Log4j 将按如下方式查找其配置源:
- 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.
- If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
- If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
- If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
- If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
- If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
- If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
- If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
- If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
- If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.
如果您尝试设置 log4j.configurationFile
成功,那么选项 1
将被启用,但该尝试不成功,我们知道这一点,因为您引用的错误消息告诉我们 Log4j 正在失败选项 10
.
我建议 运行 您的应用程序带有 -Dlog4j.configurationFile=/path/to/log4j2.xml 或确保 log4j2.xml
在您的应用程序的 class路径。用 System.setProperty(...)
调用乱丢代码的想法感觉像是一种反模式,并且可能非常脆弱,因为在 System.setProperty(...)
调用 之前 实例化的任何静态记录器将导致 Log4j 在知道您的配置文件位置之前自行初始化。