Chris
Chris

Reputation: 51

Log4j2 not creating subsequent logs files when using TestNg

I am using eclipse with testng and I am using log4j2 for the logging. When I run the tests separately, a log file is created for each test and placed in a directory that has a unique name including the datetime stamp.

For example, when I run test01, a directory is created with the datetime and a log file is placed in that directory.

2016-12-10-21-16-04 tst_01
tst_01.log

When I run test02 separately, a new directory is created based on the datetime and testname and a log file is added to the new directory.

2016-12-10-21-16-42 tst_02
tst_02.log

When I run the test suite using testng. One log file is added to the directory of the first test, but there is no log file that appears in the second directory.

Also the log file that ends up in the directory of the first test contains the log statements for the first test as well as the second test.

Code Example:

public class TestLog{
  private static final Logger TestLog = LogManager.getLogger(TestLog.class.getName());
  public static void info(String message){
    TestLog.info(message);
  }
}

Question: How do I get a new log file to be created when running the tests using testng?

Upvotes: 1

Views: 856

Answers (1)

Chris
Chris

Reputation: 51

The issue is that the log4j2.xml file was not being updated for each test that was run. The answer was to add the following to each test.

((org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false)).reconfigure();

This essentially reloads the log4j2.xml file thus picking up the new settings.

The answer was found at: http://https://stackoverflow.com/questions/28216310/reload-log4j2-configuration-on-demand

Upvotes: 2

Related Questions