我正在使用 here 中的代码在我的 Java 程序中编写日志文件
public static void main(String[] args) {
Logger logger = Logger.getLogger("MyLog");
FileHandler fh;
try {
// This block configure the logger with handler and formatter
fh = new FileHandler("C:/temp/test/MyLogFile.log");
logger.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
// the following statement is used to log any messages
logger.info("My first log");
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
logger.info("Hi How r u?");
}
我的问题是我收到多个日志文件
最佳答案
修改代码如下
fh = new FileHandler("C:/temp/test/MyLogFile.log", true);
这不会创建多个文件,而是会附加到同一个文件。
关于Java单个日志文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26944302/