Reputation: 28345
java.util.logging.Logger override (overwrites) the file data instead of add to end of file.
Is this correct? should I create 1 file for each time I initialize the app and the log system?
If not, how do I set it to write to the end of the file?
Upvotes: 4
Views: 14085
Reputation: 9436
java.util.logging.FileHandler.append=true
The append specifies whether the FileHandler
should append onto any existing files (defaults to false
).
There are other properties you have control over like the size of log files and how many to cycle through but I think that property is the one you are concerned with.
Upvotes: 13
Reputation: 4213
Hi would like to improve the answer by this code snippet.
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
public class MyTestLogger {
public static void main(String[] args) throws SecurityException,
IOException {
/*
* The below line is the syntax for the file handler which has the capability of
* appending the logs in the file. The second argument decides the appending.
* FileHandler fileTxt = new FileHandler("eLog.txt", true);
*/
FileHandler fileTxt = new FileHandler("eLog.txt", true);
SimpleFormatter formatter = new SimpleFormatter();
fileTxt.setFormatter(formatter);
Logger LOGGER = Logger.getLogger(MyTestLogger.class.getName());
LOGGER.addHandler(fileTxt);
LOGGER.setLevel(Level.SEVERE);
LOGGER.severe("This is a serious problem !");
}
}
Upvotes: 10