Reputation: 12321
I use FileHandler for logging.
Logger l = Logger.getLogger ("mylogger");
FileHandler fh = new FileHandler ("log", 1000000, 2, true);
l.addHandler (fh);
That would always log to log.0. If that reaches 1MB it gets moved to log.1. Logging continues at the empty log.0.
log.0
log.1
As far I see the numbers start always with 0. Is there a way to have no number at the current log and start he rotated log with 1?
log
log.1
Upvotes: 3
Views: 1698
Reputation: 4105
Short answer: Not using the default FileHandler
That said, there is nothing stopping you from writing your own StreamHandler. This is OpenJDK's FileHandler implementation. It's actually only a couple lines of modifications to achieve your desired behaviour.
FileHandler
actually generates all the filenames upfront in an array. You simply want to tweak the generation code such that the first element does not contain the '0'. It is extremely unfortunate that it's a private method, so you have to re-implement the whole class rather than simply extending it.
NOTE: If you are going to base your implementation on OpenJDK's, please respect the license under which it is distributed. I am not a lawyer, so you may want to ask other people of the legalities of using a modified OpenJDK source.
Now that that's out of the way, look inside the generate()
method. There are two spots that use the generation
parameter, which is the index of your logfile.
} else if (ch2 == 'g') {
word = word.append(generation); // this line
// ...and...
if (count > 1 && !sawg) {
word = word.append('.').append(generation); // this line
You want them to be guarded by if (generation > 0)
That should stop the log files from showing the 0th index on the filename.
Upvotes: 2