Brian McCormick
Brian McCormick

Reputation: 459

Java FileHandler and rolling log files

I understand how the Java FileHandler rolls to the next log file when a particular size is reached. What I want is a little different. I want to use the FileHandler to use the log file with the oldest last written time in the sequence when the program starts.

For example if I have specified to use 5 log files: mylog.0.log, mylog.1.log...mylog.4.log

If the program last updated mylog.2.log then the next time I start the program I want it to start logging to mylog.3.log.

The problem I am trying to solve is when a user executes the program and something happens they typically restart the program and if mylog.0.log is available it will always use it and not go to mylog.1.log. I lose the information from the previous execution of the program.

Upvotes: 0

Views: 2991

Answers (1)

jmehrens
jmehrens

Reputation: 11085

Per the documentation for the java.util.logging.FileHandler:

Successively older files are named by adding "0", "1", "2", etc. into the base filename.

Seems to imply that the order will always be the opposite of what you want.

Looks like your only option is to implement a config class to generate the file name you want and pass it to the FileHandler.

You can manually rollover a log file but not in the order your want.

Upvotes: 1

Related Questions