java.util.logging.Logger覆盖数据

Tom*_*ito 4 java logging

java.util.logging.Logger覆盖(覆盖)文件数据而不是添加到文件末尾.

它是否正确?每次初始化应用程序和日志系统时,我应该创建1个文件吗?

如果没有,我如何设置它写入文件的末尾?

And*_*bbs 11

java.util.logging.FileHandler.append=true

append指定是否FileHandler应附加到任何现有文件(默认为false).

FileHandler文件

您可以控制其他属性,例如日志文件的大小和循环数量,但我认为该属性是您关注的属性.


Eas*_*y K 10

您好希望通过此代码段改进答案.

   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 !");
    }
}
Run Code Online (Sandbox Code Playgroud)