edwardborner
edwardborner

Reputation: 237

When to close and remove FileHandler with java logging

I am using the java.util.logging API and atatching several FileHandlers to a named logger to write the log messages to specific files. I see that this creates a lck (lock) file for each log file. The lock file is deleted when I close and remove the FileHandler from the named Logger.

When is it best to close the FileHandler? Do I want to keep it open so that I don't have to instantiate it everytime I want to do some logging (which will result in the lock file hanging around) or should I close and recreate it each time so the lock file goes away (seems a bit heavy handed for logging).

Really a question about best practices. I have used log4j a lot so I am trying to get my head around the differences.

Thanks,

Ed

Upvotes: 4

Views: 5082

Answers (1)

seeker
seeker

Reputation: 701

As Gray mentioned, there is usually no reason to close and reopen FileHandlers.

If the .lck do not disapear after you close the program, you could try closing the Filehandlers in a Thread and add it as a Shutdown Hook with Runtime.getRuntime().addShutdownHook().

Upvotes: 2

Related Questions