tags:

views:

28

answers:

1

I wrote a custom logger where the only addition is the following method:

public static synchronized Logger getLogger(String name) {
    try {
        boolean append = true;
        FileHandler handler = new FileHandler("tmp.log", append);
        Logger      log     = Logger.getLogger(name);
        log.addHandler(handler);
        return log;
    } catch (java.io.IOException ex) {
        //Logger.getLogger(LibraLogger.class.getName()).log(Level.SEVERE, null, ex);
    } catch (SecurityException ex) {
        //Logger.getLogger(LibraLogger.class.getName()).log(Level.SEVERE, null, ex);
    }
    return Logger.getLogger(name);
}

It produces a series of log files tmp.log, tmp.log.1, tmp.log.2 etc.

How do I prevent this from happening?

+2  A: 

In your logging.properties file check that the java.util.logging.FileHandler.limit is set to 0.

From the docs:

java.util.logging.FileHandler.limit specifies an approximate maximum amount to write (in bytes) to any one file. If this is zero, then there is no limit. (Defaults to no limit).

For a rotating set of files, as each file reaches a given size limit, it is closed, rotated out, and a new file opened. Successively older files are named by adding "0", "1", "2", etc into the base filename.

Another possibility is that you are trying to write to the same log file from multiple processes. If the logger detects that a file cannot be opened (It is locked by another process) it will create a new one, by adding the next free number to it.

Romain Hippeau
I think that's what's happening, even tho the method is synchronized, the logger is detecting the file cannot be opened. Any ideas how to accomplish that?
badcodenotreat