gpt4 book ai didi

java - 使用 log4j2 配置文件设置记录器时

转载 作者:行者123 更新时间:2023-11-30 08:30:06 25 4
gpt4 key购买 nike

我已经创建了一个 log4j2.xml 文件。什么时候用于启动和配置记录器? JVM 何时启动?

最佳答案

Log4J1

来自documentation Log4J1的启动配置有两种情况:

  1. 您可以在您的应用程序中调用 BasicConfigurator.configure(); 以编程方式初始化所有配置
  2. 自动配置会查看类路径以找到配置文件。这是文档的摘录:

The log4j library does not make any assumptions about its environment.In particular, there are no default log4j appenders. Under certainwell-defined circumstances however, the static inializer of the Loggerclass will attempt to automatically configure log4j. The Java languageguarantees that the static initializer of a class is called once andonly once during the loading of a class into memory. It is importantto remember that different classloaders may load distinct copies ofthe same class. These copies of the same class are considered astotally unrelated by the JVM.

Log4J2

在 log4J2 中,他们改变了您以编程方式初始化的方式,您可以找到很多方法 here .自动方式并没有改变,所以,它是在 JVM 加载静态 block 时加载的。您可以查看配置文件的外观 here .来自documentation log4j2 的:

Log4j has the ability to automatically configure itself duringinitialization. When Log4j starts it will locate all theConfigurationFactory plugins and arrange them in weighted order fromhighest to lowest. As delivered, Log4j contains fourConfigurationFactory implementations: one for JSON, one for YAML, onefor properties, and one for XML.

关于java - 使用 log4j2 配置文件设置记录器时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41442785/

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