java.util.logging.Logger 覆盖数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2747244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.util.logging.Logger overrides data
提问by Tom Brito
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?
如果没有,如何将其设置为写入文件末尾?
回答by Andrew Hubbs
java.util.logging.FileHandler.append=true
java.util.logging.FileHandler.append=true
The append specifies whether the FileHandlershould append onto any existing files (defaults to false).
附加指定是否FileHandler应附加到任何现有文件(默认为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.
您还可以控制其他属性,例如日志文件的大小以及循环访问的数量,但我认为该属性是您所关心的。
回答by Easwaramoorthy K
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 !");
}
}

