[英]How can I append to log files in this simple Java Logging implementation?
I got the following class to create and manage a Logger
. 我有以下课程来创建和管理
Logger
。 Whenever across the code and program execution, calls to static getLogger()
catch blocks are used to log. 每当跨代码和程序执行时,都会使用对静态
getLogger()
catch块的调用进行记录。
public class Log {
private static final Logger logger = Logger.getLogger("MyLog");
public static void iniciarLog() throws IOException {
FileHandler fh;
try {
// fh = new FileHandler(System.getProperty("user.home")+System.getProperty("file.separator")+"TorrentDownloader.log");
fh = new FileHandler("%h/TorrentDownloader.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
logger.info("Se inició el log");
} catch (SecurityException | IOException e) {
logger.severe("Error al crear el log");
}
}
public static Logger getLogger() {
return logger;
}
}
However, how can I append to such logging file? 但是,如何附加到此类日志文件? All examples I've seen change a lot this implementation which I like as it's clear, brief and simple.
我见过的所有例子都改变了很多我喜欢的实现,因为它清晰,简洁。
From the FileHandler
constructor , you can specify a boolean
to specify an append mode. 从
FileHandler
构造函数中 ,您可以指定一个boolean
来指定追加模式。
Do as following: 请执行以下操作:
fh = new FileHandler("%h/TorrentDownloader.log", true);
使用不同的构造函数
fh = new FileHandler("%h/TorrentDownloader.log", true);
You can use this constructor: 您可以使用此构造函数:
FileHandler handler = new FileHandler(String pattern, boolean append);
and in your case, it's: 在你的情况下,它是:
fh = new FileHandler("%h/TorrentDownloader.log", true);
This constructor creates a FileHandler with a file name pattern, and a boolean telling whether the FileHandler should append to any existing files or not. 此构造函数创建一个带有文件名模式的FileHandler,以及一个告诉FileHandler是否应附加到任何现有文件的布尔值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.