You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by Ceki Gulcu <cg...@urbanet.ch> on 2001/01/19 13:17:31 UTC
Re: [log4j-dev] JDK1.1 cannot find log4j.properties?
Hello Calvin,
Thanks for the tip. I have modified the static initializer of log4j as follows:
/** Search for the properties file log4j.properties in the CLASSPATH. */
static {
String override
=OptionConverter.getSystemProperty(DEFAULT_INIT_OVERRIDE_KEY,
null);
// if there is no default init override, them get the resource
// specified by the user or the default config file.
if(override == null || "false".equalsIgnoreCase(override)) {
String resource = OptionConverter.getSystemProperty(
DEFAULT_CONFIGURATION_KEY,
DEFAULT_CONFIGURATION_FILE);
URL url = null;
try {
url = new URL(resource);
} catch (MalformedURLException ex) {
// so, resource is not a URL:
// attempt to get the resource from the class path
url = ClassLoader.getSystemResource(resource); <-- this works
if(url == null) {
// Is it under org/apache/log4j somewhere in the classpath?
url = Category.class.getResource(resource);
}
}
// If we have a non-null url, then delegate the rest of the
// configuration to the OptionConverter.selectAndConfigure
// method.
if(url != null) {
OptionConverter.selectAndConfigure(url, defaultHierarchy);
} else {
LogLog.debug("Could not find resource: ["+resource+"].");
}
}
}
This works in all the JDK's I have tried so far. I would just like to
mention that version 1.0 introduced the log4j.configuration system
property. The value of this property is taken as a URL to search,
overriding the search for log4j.properties from the class path. This is of
course much more flexible since the URL can point to a resource outside the
classpath, possibly on a different machine. Cheers, Ceki
ps: I am much happier with the current code.
At 12:00 19.01.2001 +1100, you wrote:
>Hello Ceki,
>
>I don't profess to know much about how java works under the cover , but I
>was having a look at the JDK javadoc for JDK1.1.8 and JDK1.2 and found the
>method:
>
> public static URL java.lang.ClassLoader.getSystemResource(String name)
>
>which is described in the JDK1.2 javadoc as:
>
> public static URL getSystemResource(String name)
> Find a resource of the specified name from the search path used to
> load classes.
> In JDK1.1, the search path used is that of the virtual machine's
> built-in class loader.
>
> Since JDK1.2, this method locates the resource through the system
> class loader (see getSystemClassLoader).
>
>
> Parameters:
> name - the resource name
> Returns:
> a URL for reading the resource, or null if the resource could not be
> found
>
>I don't know what the implications are of using this method (eg on
>performance, etc), but I inserted a call to this method in the code for
> org.apache.log4j.Category.java (v1.0.4) as follows:
>
> // if there is no default init override, them get the resource
> // specified by the user or the default config file.
> if(override == null || "false".equalsIgnoreCase(override)) {
> String resource = System.getProperty(DEFAULT_CONFIGURATION_KEY,
>
>DEFAULT_CONFIGURATION_FILE);
> URL url = null;
> try {
> url = new URL(resource);
> } catch (MalformedURLException ex) {
> // so, resource is not a URL:
> // attempt to get the resource in the most generic way:
> url = Category.class.getResource(resource);
> if(url == null) {
> // if that doen't work, then try again in a slightly
> // different way
> ClassLoader loader = Category.class.getClassLoader();
> if(loader != null) {
> url = loader.getResource(resource);
> } else {
> url = ClassLoader.getSystemResource(resource); // <<<
> NB: Code added.
> }
> }
> }
>
>
>When executing this updated class on a v1.1 JDK it was successfully able
>to find the "log4j.properties" file on the CLASSPATH.
>
>NB: The JDK versions I have found it to work on are:
>
> IBM - "JDK 1.1.8 IBM build n118p-20000322 (JIT enabled: ibmjitc V3.5
> -IBMJDK1.1-20000322)" (Windows NT4)
> Sun - "Java(TM) 2 Runtime Environment, Standard Edition (build
> 1.3.0-C) Java HotSpot(TM) Client VM (build 1.3.0-C, mixed mode)" (Windows NT4)
> IBM - "JDK 1.1.7A IBM build o117-19981120 (JIT enabled: javax)"
> (OS/2 v4)
>
>
>Regards,
>
>Calvin Chan
>
>
>
>
>
>
>****************************************************************
>NOTICE - This message is intended only for the use of the
>addressee named above and may contain privileged and
>confidential information. If you are not the intended recipient
>of this message you are hereby notified that you must not
>disseminate, copy or take any action based upon it. If you
>received this message in error please notify the Health
>Insurance Commission immediately. Any views expressed in
>this message are those of the individual sender, except where
>the sender specifically states them to be the views of the
>Health Insurance Commission.
>****************************************************************
---
Freedom as in the capacity to exercise choice.