覆盖log4j.properties [英] Override log4j.properties

查看:554
本文介绍了覆盖log4j.properties的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的java应用程序引用了一个使用log4j日志记录的第三方jar文件。问题是这个jar包含自己的log4j.properties文件,该文件在我的机器上导致访问被拒绝的异常,但是我没有控制jar文件来改变它的内容。

My java application references a 3rd-party jar file which uses log4j logging. The problem is that this jar contains its own log4j.properties file which causes access denied exceptions on my machine, but I don't have control over the jar file to change its contents.

我尝试在我的应用程序的类路径中添加自己的log4j.properties文件,但它似乎没有效果。如果我尝试使用PropertyConfigurator以编程方式导入我自己的设置,log4j似乎首先加载jar文件的属性文件(导致异常)。

I have tried adding my own log4j.properties file in my application's classpath, but it doesn't seem to have an effect. If I try to use PropertyConfigurator to import my own settings programmatically, log4j seems to load the jar file's properties file first (causing an exception).

如何短路log4j忽略第三方jar文件的log4j.properties文件并使用我自己的?

How can I short-circuit log4j to ignore a 3rd-party jar file's log4j.properties file and use my own?

推荐答案

有几种方法可以覆盖log4j .properties,其中之一是:

There are several way to override log4j.properties, one of them is:


  • 使用log4j.xml 请参阅扩展程序

  • Use log4j.xml please see the extension

另一种方法是:


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

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

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

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

  • Setting the log4j.defaultInitOverride system property to any other value then "false" will cause log4j to skip the default initialization procedure (this procedure).
  • 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".
  • Attempt to convert the resource variable to a URL.
  • 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.
  • 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.

参考: http://logging.apache.org/log4j/1.2/manual.html

这篇关于覆盖log4j.properties的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆