Set log4j log level

Using another configuration file

Perhaps you could point to another configuration file.

java -Dlog4j.configuration=config file yourApp

Where:

  • config, you file of configuration, e.g. log4j.properties or log4j.xml.
  • file, the log file, e.g. myApp.log
  • yourApp, you app, e.g. MyAppGUI

Or you can use a class

java -Dlog4j.configurationClass=config class yourApp

Where:

  • config, you file of configuration, e.g. log4j.properties or log4j.xml.
  • class, any customized initialization class, like LogManager, should implement the org.apache.log4j.spi.Configurator
  • yourApp, you app, e.g. MyAppGUI

You 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 Logger class, like public void setLevel(Level level), e.g.:

Logger.getRootLogger().setLevel(Level.TRACE);

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.


If you're using Maven, you can have two log4j configuration files:

  • one in src/main/resources, containing your production logging config
  • one in src/test/resources, containing your test-time logging config

Maven will automatically use the latter at test time, and bundle the former into your artifact (JAR, WAR, etc) so that it's used in production. You don't have to mess around with command line switches or anything.


In your junit class put:

Logger.getRootLogger().setLevel(Level.TRACE);

somewhere before the execution of the tested method. It will set the threshold level of the root logger to TRACE.