2

I'm having trouble finding my physical log files. I'm using the java.util.logging classes. I have a trivial sandbox class containing the following code:

package test;

import java.util.logging.Level;
import java.util.logging.Logger;

public class TestLogging {

public static void main(String[] args) {

@SuppressWarnings("unused")
TestLogging test = new TestLogging();   
}

public TestLogging() {

System.out.println(getClass().getClassLoader().getResource("logging.properties")); 

Logger logger = Logger.getLogger("test"); 
logger.logp(Level.CONFIG, "myClass", "myMethod", "Monday"); 
logger.logp(Level.FINE, "myClass", "myMethod", "Tuesday"); 
logger.logp(Level.FINER, "myClass", "myMethod", "Wednesday"); 
logger.logp(Level.FINEST, "myClass", "myMethod", "Thursday"); 
logger.logp(Level.INFO, "myClass", "myMethod", "Friday"); 
logger.logp(Level.SEVERE, "myClass", "myMethod", "Saturday"); 
logger.logp(Level.WARNING, "myClass", "myMethod", "Sunday");      
}
}

My logging.properties file (again, stripped of comments) is:

handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level= ALL
java.util.logging.FileHandler.pattern = %h/java%u.log
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
x.y.level = SEVERE
x.y.z.level = WARNING
org.sscce.level = WARNING
org.sscce.baz3.level = INFO

The logging.properties file is in the classpath under User Entries in the Run Configuration for this class.

When I execute the class, I get this on the console:

file:/C:/Program%20Files/Java/jdk1.6.0_18/jre/lib/logging.properties
28-Feb-2012 2:05:55 PM myClass myMethod
INFO: Friday
28-Feb-2012 2:05:56 PM myClass myMethod
SEVERE: Saturday
28-Feb-2012 2:05:56 PM myClass myMethod
WARNING: Sunday

I asked for the logging level on the console to be INFO and that's all I have on the console so that part seems to be working. But when I look for my physical java logs in %h, which is C:\Documents and Settings[My Windows ID], there are no files that follow the pattern javaX.log where X is an integer.

What am I doing wrong? Is the program not seeing the logging.properties file? Or are the values set in some way to prevent any physical log from being written?

I need some help in debugging this because I can't see anything obviously wrong. Any takers?

Anonymous Coward
  • 479
  • 3
  • 6
  • 10
  • 1
    you realize `paths with spaces + Java == pain and suffering`! –  Feb 28 '12 at 19:34
  • possible duplicate of [How to set up java logging using a properties file? (java.util.logging)](http://stackoverflow.com/questions/960099/how-to-set-up-java-logging-using-a-properties-file-java-util-logging) –  Feb 28 '12 at 19:43

1 Answers1

2

To find your log files, you need to know that %h stands for the value in your user.home variable.

To get that, execute: System.out.println("%h: " + System.getProperty("user.home"));

You have to set which properties file you are using with a System.property as well. Here is one way to do it, otherwise it uses a default that isn't where you probably think it is.

java -Djava.util.logging.config.file=mylogging.properties

You can also do it programmatically before you start using the logging classes.

also

paths with spaces + Java == pain and suffering

and always use / as the path separator, Java will do the correct thing, even on Windows.

  • I've bumped my head on a path-with-embedded-space issue from time to time but it's been a while. I suppose that may be it. I can prove that by copying the logging.properties to somewhere without an embedded space, like C:\ and try it again. I didn't realize that I had to add the java -D parameter to the application parameters as well. I'll try that too. Can you remind me of something? If I want to specify the full path to the properties file in Windows, can I use C:\myDir\logging.properties or do I have to double the backslashes and put the whole thing in quotes just to be sure? – Anonymous Coward Feb 28 '12 at 19:54
  • always use `/` in Java for path separators, even in Windows, it works it out. Wrap everything in `"` just to be safe. –  Feb 28 '12 at 19:58
  • Never mind! I copied logging.properties to C:\ and wrote the parameter exactly like this in the VM parameters: -Djava.util.logging.config.file=C:\logging.properties. The code works perfectly now and I have a log showing all 7 days of the week exactly where it is supposed to be. Thanks Jarrod! Now I know what I need to do to make this work the way I want. – Anonymous Coward Feb 28 '12 at 19:58
  • Always use quotes, always use forward slashes. Okay, will do Jarrod. Thanks again! – Anonymous Coward Feb 28 '12 at 20:01