1

My program (a server) has a logger, it is an attribute of the main server class and it is used by all the other classes to log an error. Is there a way to delete the .lck files when the program terminates? My code:

private static Logger log = Logger.getLogger("ServerLog");

public Server() {
    // initialize variables
    try {
        FileHandler fh = new FileHandler(
                "src/main/resources/log/ServerLog.log", true);
        log.addHandler(fh);
        log.setLevel(Level.ALL);
        fh.setFormatter(new SimpleFormatter());
    } catch (SecurityException e) {
        getLog().log(Level.WARNING, e.getMessage());
    } catch (IOException e) {
        getLog().log(Level.WARNING, e.getMessage());
    }
}

And it is called by other classes to log errors.

Second question: when I write on a file like:

public final void saveRanking() {
    try {
        FileOutputStream fos = new FileOutputStream(
                "src/main/resources/database/dataRanking.out");
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(getRanking());
        oos.flush();
        oos.close();
        fos.close();
    } catch (FileNotFoundException e) {
        getLog().log(Level.SEVERE, e.getMessage());
    } catch (IOException e) {
        getLog().log(Level.SEVERE, e.getMessage());
    }
}

is there a way to ensure that the file is saved without problem even if the process terminates during the writing?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Simon
  • 5,070
  • 5
  • 33
  • 59
  • 1
    I'm not sure I understand what creates the .lck. – Jochen Bedersdorfer Jun 08 '11 at 01:33
  • 1
    One tip on *nix, directory creation is an atomic process (i.e. the inode either exists or it doesn't, universally) so you use a lock-directory instead of a lock-file, coz it's that bit safer. To answer (one of) you question: Make your Logger class Closable and close it somewhere which is ALLWAYS invoked on program exit, like a finally block in the main method, just for instance. There's no way to ENSURE anything in a program, we try, we catch, and we finalize. Sigh. – corlettk Jun 08 '11 at 01:37
  • don't ask two unrelated questions in one Question. I recommend that you edit the Question to remove the second one ... and create a new Question for it. – Stephen C Jun 08 '11 at 04:58

1 Answers1

0

If your code is removing the FileHandler on shutdown, be sure to close the FileHandler after you remove it. You should expect to see lock files when the FileHandler is open. If you see them linger after the VM exits, then it is because the The FileHandler was not closed, the VM was halted or crashed while the handler shutdown hook was running, or an I/O exception took place while trying to remove them.

You could be running into JDK-6774110 lock file is not deleted when child logger is used. There is no evaluation on this but, I think this could happen when a logger is garbage collected. See JDK-6274920: JDK logger holds strong reference to java.util.logging.Logger instances.

jmehrens
  • 10,580
  • 1
  • 38
  • 47