简体   繁体   English

java.util.logging.Logger覆盖数据

[英]java.util.logging.Logger overrides data

java.util.logging.Logger override (overwrites) the file data instead of add to end of file. java.util.logging.Logger覆盖(覆盖)文件数据而不是添加到文件末尾。

Is this correct? 这个对吗? should I create 1 file for each time I initialize the app and the log system? 每次初始化应用程序和日志系统时,我应该创建1个文件吗?

If not, how do I set it to write to the end of the file? 如果没有,我如何设置它写入文件的末尾?

java.util.logging.FileHandler.append=true

The append specifies whether the FileHandler should append onto any existing files (defaults to false ). append指定FileHandler是否应附加到任何现有文件(默认为false )。

FileHandler Doc FileHandler文件

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. 您可以控制其他属性,例如日志文件的大小和循环数量,但我认为该属性是您关注的属性。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM