[英]How can i skip appium server log from my eclipse console to txt file
[英]How can i log my Java Console output to Log1.txt, Log2.txt etc?
我如何将Java控制台输出记录到Log1.txt,Log2.txt等? 我目前正在使用此代码:
PrintStream out = new PrintStream(new FileOutputStream("output.txt"));
System.setOut(out);
但这代替了我不想要的旧日志。 我想要它,例如,如果有一个log1.txt,我希望它登录到log2.txt,而不是替换log1.txt。如果有人可以帮助我,那就太好了!
为避免覆盖现有的日志文件,您必须进行测试以查看日志文件是否已存在,或者更好的方法是,维护一个计数器来跟踪已创建的日志文件的数量。
在某个地方,您必须拥有类似的东西:
int logCount = 1;
PrintStream out = new PrintStream(new FileOutputStream("output" + String.valueOf(logCount) + ".txt"));
// Remember to increment the counter after the log file is written
logCount++;
您可以使用如下形式:
try {
String outputFileName = "output.txt";
File file = new File(outputFileName);
for(int i = 0; file.exists(); i++){
file = new File("output"+i+".txt");
}
PrintStream out = new PrintStream(new FileOutputStream(file));
System.setOut(out);
} catch (IOException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.