Changing the log level with Pax Exam

I am currently working with Pax Exam to test a tiny OSGi application.
This tool comes with two log configurations: one for the driver (what launches the test process) and one for the container.

The fact is that the log outputs I had in my tests were quite important and detailed. By default, Pax Exam logs are configured with the DEBUG level. So, I had to change the log level, and I wanted to do it before the @Configuration is loaded. Although the log configuration is explained on this page, I generally appreciate this little thing that makes me gain some time. Even if we talk about 10 minutes.

So, assuming you used Logback instead of log4j (check your POM), all you need to change the log level is to create the file src/test/resources/logback.xml and to add this content.

<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright 2010 Harald Wellmann

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0
-->
<configuration>

  <!-- log to System.out on console -->
  <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  
  <!-- log to file test.log -->
  <appender name="TEST_LOG" class="ch.qos.logback.core.FileAppender">
    <file>test.log</file>
    <encoder>
      <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
    </encoder>
  </appender>
  
  <root level="WARN">
    <appender-ref ref="STDOUT"/>
    <appender-ref ref="TEST_LOG"/>
  </root>
  <logger name="org.ops4j.pax.exam" level="INFO"/>
  
</configuration>

Although you can configure it differently, this is a very good basis. And if you used log4j, simply add a log4j.properties file as shown here.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s