views:

2061

answers:

3

Hi,

I'm trying to load a custom log.properties file when my application is started.

My properties file is in the same package as my main class, so i assumed that the -Djava.util.logging.config.file=log.properties command line parameter should get the properties file loaded.

But the properties are only loaded when i specify a full absolute path to the properties file. Any suggestions how to use a relative path?

Thanks in advance.

A: 

I would suggest to take a look at the Apache Logging Services Project. To my mind it's far easier to use and to configure. Moreover it's kind of an 'industry standard' in java.

Oliver Michels
I guess it used to be the industry standard, before Sun included the unfortunate java.util.logging in the JDK. Now you have log4j, j.u.l, Jakarta Commons Logging, SLF4J, logback and others. Logging in Java is really messed up :-(
Thilo
+2  A: 

Java logging doesn't search your whole hard disk for a file; there are very simple rules how files are looked up. You want Java to see that the two files belong to each other but you didn't say so anywhere. Since Java sees no connection between the properties file and your class other than that they are in the same folder on your disk, it can't find the file.

In your case, you do this:

  • Move the file log.properties to the default package (the root folder for your sources)
  • add it directly to the classpath (just like a JAR)
  • You can specify the package in which the file is, replacing "." with "/": -Djava.util.logging.config.file=com/company/package/log.properties
  • You can specify the absolute path
Aaron Digulla
Hi, thanks for the answer. I'm just beginning with java, so i don't know a lot about packaging etc.I have a NetBeans default project with a package called javaapplication.The log.properties file is in the default package now.So my command line looks like this:JavaApplication\dist>javaw -Djava.util.logging.config.file=log.properties -jar JavaApplication.jarThe properties file still isn't loaded. Could you give me an additional hint please? :)
abp
Are you sure it's not loaded? "javaw" swallows all output to System.out and System.err. Try "java" instead (without "w"). Also, you can try to debug the code to see why it's not logging. Just set a breakpoint and "step into" the logging call (instead of "step over"). log4j offers a debug option which makes it print the config as it reads it; I don't know if you can do the same with java.util.logging.
Aaron Digulla
Thanks for the reply. I think i'll stick to runtime configuration through a class, because i can't get the properties file to load with a relative path.
abp
+1  A: 

util logging does not load from classpath, it needs an absolute path which is why other logging packages like log4j are far easier to configure and better for web apps where it's a pain to get abs paths.

this is not explained at all in the java.util.logging.LogManager doco.