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

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

问题描述

我的 java 应用程序引用了一个使用 log4j 日志记录的 3rd-party 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 以忽略第 3 方 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 请看扩展

另一种方法是:

  • log4j.defaultInitOverride 系统属性设置为任何其他值那么false"将导致 log4j 跳过默认的初始化过程(这个过程).
  • 将资源字符串变量设置为 log4j.configuration 系统属性的值.指定默认初始化文件的首选方法是通过 log4j.configuration 系统属性.如果未定义系统属性 log4j.configuration,则将字符串变量资源设置为其默认值log4j.properties".
  • 尝试将资源变量转换为 URL.
  • 如果资源变量无法转换为 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天全站免登陆