I am using java.util.logging for logging my program, the problem is that I need to create separate log file for each instance of class test case. For example, I have three test case objects, and in the end I get three log files, but:
Test Case #3 contains log for test case #3, Test Case #2 contains logs for test cases 2 and 3, and test case #1 contains log of all test cases.
Here is my code:
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();
}
}
problem is in
log.addHandler(fh);
its keep on adding the handler. so the behavior is as you are seeing. You should use
fh.close();
log.removeHandler(fh);
after executing test case.