Java.util.logging.Logger overrides data

java.util.logging.Logger overrides (overwrites) file data instead of appending to the end of the file.

It is right? Do I have to create 1 file for each time the application and log system are initialized?

If not, how do I configure it to write to the end of the file?

+4
source share
2 answers

java.util.logging.FileHandler.append=true

The append application indicates whether to add a FileHandler to any existing files ( false by default).

Filehandler doc

There are other properties that you control, such as the size of the log files and how many cycles, but I think this is the property you are worried about.

+10
source

Hi, I would like to improve the answer with this piece of code.

  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 !"); } } 
+7
source

Source: https://habr.com/ru/post/1308500/


All Articles