I’m currently working on a project that uses log4j.
I’m running a testcase (junit) and would like to set the log level to trace so that I can see if all the values are correct. Classes that use logging in the project contain a line like the following:
private static final Log LOG = LogFactory.getLog(MatchTaskTest.class);
and use a like like this to do the actual debugging
LOG.trace("value");
I have never used log4j before, does anybody know how I can change the log level just for the testcase, preferably simply by defining a parameter in eclipse’s run configuration dialog.
Using another configuration file
Perhaps you could point to another configuration file.
Where:
config, you file of configuration, e.g.log4j.propertiesorlog4j.xml.file, the log file, e.g.myApp.logyourApp, you app, e.g.MyAppGUIOr you can use a class
Where:
config, you file of configuration, e.g.log4j.propertiesorlog4j.xml.class, any customized initialization class, likeLogManager, should implementthe
org.apache.log4j.spi.ConfiguratoryourApp, you app, e.g.MyAppGUIYou can see more in Apache log4j 1.2 – Short introduction to log4j on Default Initialization Procedure section.
Modifying the level programmatically
Moreover, you can also use the methods that offers the
Loggerclass, likepublic void setLevel(Level level), e.g.:Since you want only for testing purposes, you could use them. But it is recommended not to use in client code because they overwrite the default configuration parameters in hard coded. The best way is to use an external configuration file.