I want to use the new Log4J 2 - Java Logging Framework. Everything work fine, but I tried since a hour to load a custom configuration file to configure the logging (like log
My 2 cents:
Software system: log4j 2, NetBeans and TestNG.
I had the same problem, but with the testing environment.
In default, (that is, in log4j2.xml file located in src/) the logging level is set to error. But when running the tests, I want the logging to be trace. And, of course logged to some more or less hardcoded file.
That is, originally I did something like:
public class someTestClass {
static final Logger log=LogManager.getLogger("test.someTestClass");
......
@Test
public void SomeMethod(){
System.setProperty("log4j.configurationFile", "resources/log4j2_conf.xml");
log.trace("This will not show because the default logging level is ERROR
and the resources/log4j2_conf.xml is not loaded");
}
The problem is that by the time the System.setProperty instruction will be executed, the log4j will be already be set (That is, the static final Logger log =... will be executed first.)
What I did, is use the @BeforeClass like this:
public class someTestClass {
Logger log;
@BeforeClass
public void setLogger(){
SetEnv.SetEnvironment();
log = LogManager.getLogger("TestRealClientRealServer");
}
......
@Test
public void SomeMethod(){
log.trace("This will show, because in log4j2_conf.xml
the root level is set to TRACE");
}
}
BTW, resources/ can be placed in test packages so you don't ship the "test" log4j setup.
Hope it helps