1

How to write logs in text file but do not overwrite the file?

Logger logger = Logger.getLogger("MyLog");  
FileHandler fh = new FileHandler("C:/temp/test/MyLogFile.log");  
logger.addHandler(fh);
...
logger.info("log sample");
  • possible duplicate of [How to append text to an existing file in Java](http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java) – Mordechai Jul 30 '15 at 19:16

1 Answers1

4

Change this :

FileHandler fh = new FileHandler("C:/temp/test/MyLogFile.log");  

to this

FileHandler fh = new FileHandler("C:/temp/test/MyLogFile.log", true);  

If you read the documentation you will see that the second argument specifies the optional append argument. When true you will write at the end of the (existing) file, without overwriting it.

See documentation

Kevin
  • 2,813
  • 3
  • 20
  • 30