Question

I'm using java.util.logging for a project and am having a problem with the configuration. I have a logging.properties file that I know is being read because e.g. changes to level and filehandler pattern take effect.

The problem is that even though I attempt to force the existence of only one log file java.util.logging insists on creating additional ones. I have read the docs and here is an example configuration for the FileHandler:

java.util.logging.FileHandler.pattern = server.log
java.util.logging.FileHandler.limit = 500000
java.util.logging.FileHandler.count = 1
java.util.logging.FileHandler.append = false
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter

The result is that files get created as server.log.1, server.log.2 etc. Ideally I don't want append to be true (does not seem to make a difference...), I just want a new log file to be created every time I start my program, potentially overwriting an existing file. At any time, there should be no more than count log files in my directory.

Changing append and count does not seem to make any difference. Using %u and %g etc. in the pattern does not seem to make much of a difference either. I still keep getting server.log.n files which clutters the directory.

Each created file also has an accompanying .lck file. Why are they created and could they be the culprit?

Edit:

Don't know why I didn't try this before, but manually removing the .lck files results in things working as desired. So the question now really is: why are .lck files created (improper shutdown?) and how can that be handled?

Was it helpful?

Solution

This only happens when the FileHandler detects that the server.log file is locked. You either have two JVMs running at the same time, two instances of a FileHandler open at the same time pointing to the same file name, or another process is locking the server.log file before the FileHandler is opened.

Related: Is java.util.logging.FileHandler in Java 8 broken?, JavaLogger randomly writes to a second file and FileLock.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top