开发者

Override log4j.properties

开发者 https://www.devze.com 2023-01-26 19:12 出处:网络
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, b

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.

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).

How can I short-circuit log4j to ignore a 3rd-party jar file's log4j.properties file and use m开发者_如何学运维y own?


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

  • Use log4j.xml please see the extension

Another approach is:

  • 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.

Ref: http://logging.apache.org/log4j/1.2/manual.html


If none of these works

Add this following statement in your app configuration

BasicConfigurator.configure();

it worked for me


Try adding an environment variable called LOG4J_CONFIGURATION_FILE while you start the application.

0

精彩评论

暂无评论...
验证码 换一张
取 消