我使用java.util.logging来记录我的程序,问题是我需要为每个类测试用例的实例创建单独的日志文件。例如,我有三个测试用例对象,最后我得到三个日志文件,但是:
测试用例#3包含测试用例#3的日志,测试用例#2包含测试用例2和3的日志,测试用例#1包含所有测试用例的日志。
这是我的代码:
public class TestCase {
TestCase(String tcName){
this.tcName = tcName;
}
Logger log = Logger.getLogger("com.sigmaukraine.trn.autotest.testcase");
String tcName;
String scenarioReportDir;
List<Keyword> kwList = new ArrayList<Keyword>();
public void executeTestCase(){
//saving log for current test case
try {
FileHandler fh;
String fileName = new StringBuilder(tcName).append(".log").toString();
// This block configure the logger with handler and formatter
fh = new FileHandler(scenarioReportDir + fileName);
log.addHandler(fh);
SimpleFormatter formatter = new SimpleFormatter();
fh.setFormatter(formatter);
log.info("Executing test case: " + tcName);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for(Keyword k : kwList){
k.executeKeyword();
}
}
答案 0 :(得分:0)
问题在
log.addHandler(fh);
继续添加处理程序。所以行为就像你所看到的那样。你应该使用
fh.close();
log.removeHandler(fh);
执行测试用例后。
答案 1 :(得分:-3)
<appender name="RootSiftAppender" class="ch.qos.logback.classic.sift.SiftingAppender">
<discriminator>
<Key><strong>testname</strong></Key>
<DefaultValue><strong>testrun</strong></DefaultValue>
</discriminator>
<sift>
<appender name="FILE-${testname}" class="ch.qos.logback.core.rolling.RollingFileAppender">
<File>${testname}.log</File>
<rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
<FileNamePattern><strong>${testname}.%i.log</strong></FileNamePattern>
<MinIndex>1</MinIndex>
<MaxIndex>100</MaxIndex>
</rollingPolicy>
<triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">
<MaxFileSize>10MB</MaxFileSize>
</triggeringPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{ISO8601} %-5level %C{1} [%M:%L] [%thread] - %msg%n</Pattern>
</layout>
</appender>
</sift>
</appender>