Logging Configuration

Default Configuration with Pax Logging

Newcomers often wonder why Pax Exam produces either too much or too little log output or why a change in the logging configuration does not have the expected effect.

To understand logging in Pax Exam, keep in mind that logging needs to be configured separately for the test driver and the test container, which may even run in separate processes.

Pax Exam uses the SLF4J logging API, which requires a static binding to a logging backend to produce any output at all.

By default, Pax Exam provisions Pax Logging to the test container. Pax Logging provides both an org.slf4j package and an org.slf4j.impl binding implemented by log4j. There is currently no way to use an alternative binding with Pax Logging.

For the driver, you can also use Pax Logging as a plain old JAR, or use the official slf4j-api JAR together with a binding of your choice. Note that this will not affect logging inside the test container.

Using Log4J2 classes in Pax Exam tests

As per the above, by default, pax-exam will provide the standard API for logging via SLF4J API. If your code makes direct calls to the log4j 2.x API, you need to explicitly provision more bundles:

Typical log4j2 provisioning
mavenBundle().groupId("org.apache.logging.log4j")
         .artifactId("log4j-api")
         .versionAsInProject(),
 mavenBundle()
         .artifactId("pax-logging-log4j2")
         .groupId("org.ops4j.pax.logging")
         .versionAsInProject(),
 mavenBundle()
         .artifactId("pax-logging-api")
         .groupId("org.ops4j.pax.logging")
         .versionAsInProject(),

 

Overriding Pax Logging with SLF4J and Logback

If you prefer SLF4J and Logback to Pax Logging and log4j, you can override the defaults of Pax Exam as follows:

  • Add a configuration option to your exam.properties:
pax.exam.logging = none
  • Add provisioning options for the SLF4J API and any compatible backend of your choice, e.g.
mavenBundle("org.slf4j", "slf4j-api", "1.6.1"),
mavenBundle("ch.qos.logback", "logback-core", "1.0.6"),
mavenBundle("ch.qos.logback", "logback-classic", "1.0.6")
  • Make sure that the driver and the test container use the same logging configuration by adding another option
systemProperty("logback.configurationFile")
    .value("file:" + PathUtils.getBaseDir() + "/src/test/resources/logback.xml")