Java 配置 Log4j XML 配置的方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18814563/
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 11:18:18  来源:igfitidea点击:

Ways to configure Log4j XML Configuration

javaweb-applicationslog4j

提问by srk

I am using log4j to log information in my web application. I have chosen log4j.xml type of configuration instead of log4j.properties. I remember that, for log4j.properties configuration, I did not write Java code lines to find the log4j.property file location. However, for log4j.xml file configuration I am specifying explicitly to find it like this

我正在使用 log4j 在我的 Web 应用程序中记录信息。我选择了 log4j.xml 类型的配置而不是 log4j.properties。我记得,对于 log4j.properties 配置,我没有编写 Java 代码行来查找 log4j.property 文件位置。但是,对于 log4j.xml 文件配置,我明确指定要像这样找到它

DOMConfigurator.configure("log4j.xml");//it reads file from classpath, working!

Also, tested my application removing the above statement from source. It doesn't see to work. None of the debug statements were printed except

另外,测试了我的应用程序,从源代码中删除了上述语句。它看起来不起作用。没有打印任何调试语句,除了

System.out.println();

System.out.println();

I have read that log4j by default looks for log4jproperties file or log4j.xml file in the classpath. I checked in the deployed web application, log4j.xml file is in web-inf/classes. Even though it can't find log4j.xml.

我已经读过 log4j 默认在类路径中查找 log4jproperties 文件或 log4j.xml 文件。我检查了部署的 Web 应用程序,log4j.xml 文件在 web-inf/classes 中。即使它找不到 log4j.xml。

Is it that, the above code line is mandatory for log4j xml configuration in Java? In fact, can't Java source code doesn't pickup log4j.xml from the classpath without explicit specification as above?

是不是,上面的代码行对于 Java 中的 log4j xml 配置是强制性的?事实上,Java 源代码不能在没有明确说明的情况下从类路径中提取 log4j.xml 吗?

采纳答案by eis

From log4j manual:

log4j 手册

The exact default initialization algorithm is defined as follows:

  1. Setting the log4j.defaultInitOverride system property to any other value then "false" will cause log4j to skip the default initialization procedure (this procedure).

  2. Set the resource string variable to the value of the log4j.configuration system property. The preferred way to specify the default initialization file is through the log4j.configuration system property. In case the system property log4j.configuration is not defined, then set the string variable resource to its default value "log4j.properties".

  3. Attempt to convert the resource variable to a URL.

  4. If the resource variable cannot be converted to a URL, for example due to a MalformedURLException, then search for the resource from the classpath by calling org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) which returns a URL. Note that the string "log4j.properties" constitutes a malformed URL. See Loader.getResource(java.lang.String) for the list of searched locations.

  5. If no URL could not be found, abort default initialization. Otherwise, configure log4j from the URL. The PropertyConfigurator will be used to parse the URL to configure log4j unless the URL ends with the ".xml" extension, in which case the DOMConfigurator will be used. You can optionaly specify a custom configurator. The value of the log4j.configuratorClass system property is taken as the fully qualified class name of your custom configurator. The custom configurator you specify must implement the Configurator interface.

确切的默认初始化算法定义如下:

  1. 将 log4j.defaultInitOverride 系统属性设置为任何其他值,然后“false”将导致 log4j 跳过默认初始化过程(此过程)。

  2. 将资源字符串变量设置为 log4j.configuration 系统属性的值。指定默认初始化文件的首选方法是通过 log4j.configuration 系统属性。如果未定义系统属性 log4j.configuration,则将字符串变量资源设置为其默认值“log4j.properties”。

  3. 尝试将资源变量转换为 URL。

  4. 如果资源变量无法转换为 URL,例如由于 MalformedURLException,则通过调用返回 URL 的 org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) 从类路径中搜索资源. 请注意,字符串“log4j.properties”构成了格式错误的 URL。有关搜索位置的列表,请参阅 Loader.getResource(java.lang.String)。

  5. 如果找不到 URL,则中止默认初始化。否则,从 URL 配置 log4j。PropertyConfigurator 将用于解析 URL 以配置 log4j,除非 URL 以“.xml”扩展名结尾,在这种情况下将使用 DOMConfigurator。您可以选择指定自定义配置器。log4j.configuratorClass 系统属性的值被视为自定义配置器的完全限定类名。您指定的自定义配置器必须实现 Configurator 接口。

So, without code, you can (and should!) define the system property to tell where your config file is. If not, it by default will search for log4j.properties in the classpath. That is the only file it is searching for by default.

因此,无需代码,您就可以(并且应该!)定义系统属性来告诉您的配置文件在哪里。如果没有,它默认会在类路径中搜索 log4j.properties。这是它默认搜索的唯一文件。

System property is usually defined as a startup parameter for your app (app server), something like -Dlog4j.configuration=path/to/your/config.xml.

系统属性通常定义为您的应用程序(应用程序服务器)的启动参数,例如-Dlog4j.configuration=path/to/your/config.xml.