Java 覆盖 log4j.properties
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4258849/
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
Override log4j.properties
提问by Isaac Sutherland
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.
我的 java 应用程序引用了一个使用 log4j 日志记录的 3rd-party jar 文件。问题是这个 jar 包含它自己的 log4j.properties 文件,这会导致我机器上的访问被拒绝异常,但我无法控制 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.properties 文件,但它似乎没有效果。如果我尝试使用 PropertyConfigurator 以编程方式导入我自己的设置,log4j 似乎首先加载 jar 文件的属性文件(导致异常)。
How can I short-circuit log4j to ignore a 3rd-party jar file's log4j.properties file and use my own?
如何短路 log4j 以忽略第 3 方 jar 文件的 log4j.properties 文件并使用我自己的文件?
采纳答案by Puspendu Banerjee
There are several way to override log4j.properties, one of them is:
有几种方法可以覆盖 log4j.properties,其中之一是:
- Use log4j.xml please see the extension
- 使用 log4j.xml请看扩展名
Another approach is:
另一种方法是:
- Setting the log4j.defaultInitOverridesystem 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.
- 将log4j.defaultInitOverride系统属性设置为任何其他值,然后“false”将导致 log4j 跳过默认初始化过程(此过程)。
- 将资源字符串变量设置为 log4j.configuration 系统属性的值。指定默认初始化文件的首选方法是通过 log4j.configuration 系统属性。如果未定义系统属性 log4j.configuration,则将字符串变量资源设置为其默认值“log4j.properties”。
- 尝试将资源变量转换为 URL。
- 如果资源变量无法转换为 URL,例如由于 MalformedURLException,则通过调用返回 URL 的 org.apache.log4j.helpers.Loader.getResource(resource, Logger.class) 从类路径中搜索资源. 请注意,字符串“log4j.properties”构成了格式错误的 URL。有关搜索位置的列表,请参阅 Loader.getResource(java.lang.String)。
- 如果找不到 URL,则中止默认初始化。否则,从 URL 配置 log4j。PropertyConfigurator 将用于解析 URL 以配置 log4j,除非 URL 以“.xml”扩展名结尾,在这种情况下将使用 DOMConfigurator。您可以选择指定自定义配置器。log4j.configuratorClass 系统属性的值被视为自定义配置器的完全限定类名。您指定的自定义配置器必须实现 Configurator 接口。
回答by harsha kumar Reddy
If none of these works
如果这些都不起作用
Add this following statement in your app configuration
在您的应用程序配置中添加以下语句
BasicConfigurator.configure();
BasicConfigurator.configure();
it worked for me
它对我有用
回答by Dragos Ionut
Try adding an environment variable called LOG4J_CONFIGURATION_FILE while you start the application.
尝试在启动应用程序时添加一个名为 LOG4J_CONFIGURATION_FILE 的环境变量。