Java Logging - where is my log file?

2019-02-11 12:14发布

I'm having trouble finding my log files.

I'm using Java Logging - java.util.logging - in Eclipse 3.7.1 on Windows XP. The relevant lines of my logging.properties file are:

handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level=INFO
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

As far as I can figure out, after I execute these two lines:

Logger logger = Logger.getLogger("test"); 
logger.logp(Level.INFO, "myClass", "myMethod", "Alcatraz"); 

my log file should be in C:\Documents and Settings\[My Windows ID]\javaX.log where X is an integer.

I have 5 different java.log files in that directory, java0.log through java4.log, but none of them contain my log record or even a record with today's date on it. I did some googling and found Tracing and Logging which implies that my logs should be at a different location, c:\Documents and Settings\[My Windows ID]\Application Data\Sun\Java\Deployment\log. There is one file there, named plugin5581819941091650582.log, but it is essentially empty:

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
</log>

Its creation date is last week. (I'm not sure what process created it; I certainly didn't create it explicitly.)

So where is my log file then? I can't think of anywhere else to look.

Also, does anyone know when changes to logging.properties take effect? If I changed the log level or the FileHandler.pattern, what would have to happen before my program saw the changes? Simply saving the changes in logging.properties is clearly not enough. Will I need to restart Eclipse? Or reboot the computer? Just curious. That's not nearly as big a deal to me as finding out where my log file actually is.

7条回答
▲ chillily
2楼-- · 2019-02-11 13:04
  1. Debug the your variable or logger.getHandlers(): just for the instances of FileHandler, and look for its private field: files
  2. Make sure that your logger level including your log.
  3. Make sure that your handler level including your log.
  4. It will be sent to your home directory. If there's no that directory in your operate system, such as windows 95/98/ME, the file should be saved to the default path like C:\Windows\.
  5. Reflect, the same as tip 1

    Field[] handlerFiles = handler.getClass().getDeclaredFields();
    AccessibleObject.setAccessible(handlerFiles, true);
    
    for (Field field : handlerFiles) {
        if (field.getName().equals("files")) {
            File[] files = (File[]) field.get(handler);
            System.out.println(Arrays.toString(files));
        }
    }
    
  6. The log manager will be initialized during JVM startup and completed before the main method. You can also re-initialize it in main method with System.setProperty("java.util.logging.config.file", file), which will call LogManager.readConfiguration().

查看更多
登录 后发表回答