Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

SWTBot/MigrationToSLF4J


SWTBot
Website
Update Sites
Community
Mailing List
Forums/Newsgroups
IRC
Contribute
Open Bugzilla tickets
Open Gerrit reviews
Browse Source
Continuous Integration


Migration to SLF4J logging

SWTBot 4.0.0 has removed its use of org.apache.log4j and is instead using the org.slf4j.api facade for its logging. Migration to SLF4J gives the flexibility of using any of the logger, e.g. simple, log4j, logback, etc.


The users of SWTBot have to include one of the SLF4J Binders to get log messages from SWTBot tests.

The following messages will appear after running the test, when there is no SLF4J Binding-

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation

At this point, you have to start using SLF4J Binder.

This section illustrates one set of combinations that are tested with Eclipse 2022-06. There can be various such combinations.

Note- The versions of slf4j binding jars may vary depending upon the Eclipse version. It is important to pick the jars compatible to your Eclipse version from Orbit downloads.

Do not include multiple loggers at the same time in your application.

For simplicity, lib folder in the examples below is the folder in the plugin that contains the jars that are required in classpath. You may use any mechanism to get them in classpath.

Simple Logger-

1. Install SLF4J Simple Binding from Orbit. It will install org.slf4j.binding.simple jar.

2. MANIFEST.MF

 Import-Package: org.slf4j
 Bundle-ClassPath: .,
 lib/org.slf4j.api_1.7.30.v20200204-2150.jar,
 lib/org.slf4j.binding.simple_1.7.30.v20200204-2150.jar

3. Add simplelogger.properties at the root of the plugin.

4. Test code should define logger as:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SLF4JTest {
public static final Logger logger = LoggerFactory.getLogger(SLF4JTest.class); }


5. PDE SWTBot Run launch

Arguments tab can have level “-Dorg.slf4j.simpleLogger.defaultLogLevel=debug” if you want debug level messages.

Plugins tab- Include org.slf4j.binding.simple and org.slf4j.api along with other required plugins.


Log4j logger-

1. Install SLF4J log4j Binding and org.apache.log4j from Orbit. It will install org.slf4j.binding.log4j12 and org.apache.log4j jars. The versions of org.apache.log4j, log4j binder and slf4j should be compatible.

org.slf4j.impl.log4j12 is also one option that works with older versions of Eclipse.

Do not use multiple implementations and bridges of log4j; otherwise it will cause StackOverflow.

2. MANIFEST.MF

 Import-Package: org.slf4j
 Bundle-ClassPath: .,
 lib/org.apache.log4j_1.2.19.v20220208-1728.jar,
 lib/org.slf4j.api_1.7.30.v20200204-2150.jar,
 lib/org.slf4j.binding.log4j12_1.7.30.v20201108-2042.jar

3. Add log4j.properties at the root of the plugin. If you want to get debug messages in log, you need to set ‘threshold’ property of the Appender.

4. Define logger in your test code as above.

5. PDE SWTBot Run launch

Plugins tab- Include org.slf4j.binding.log4j12 , org.apache.log4j and org.slf4j.api along with other required plugins.


Logback logger-

1. Install Logback SLF4J Binding, Logback Classic Module and Core Module from Orbit. It will install ch.qos.logback.classic, ch.qos.logback.Core and ch.qos.logback.slf4j

2. MANIFEST.MF

 Require-Bundle: org.eclipse.swtbot.go,
 ch.qos.logback.classic,
 ch.qos.logback.core
 Import-Package: org.slf4j
 Bundle-ClassPath: .,
 lib/ch.qos.logback.slf4j_1.2.3.v20200428-2012.jar


3. Define logger in your test code as above.

4. PDE SWTBot Run launch

Plugins tab- Include ch.qos.logback.classic, ch.qos.logback.core, ch.qos.logback.slf4j and org.slf4j.api along with other required plugins.

Enabling SLF4J logging in Maven/CI

To enable SLF4J logging from SWTBot using Maven or in CI environment, the following steps can be used:

1. Add slf4j.api and chosen binding in target, for example from Orbit:

 <location includeAllPlatforms="false" includeConfigurePhase="false" includeMode="planner" includeSource="true" type="InstallableUnit">
 <unit id="org.slf4j.api" version="0.0.0"/>
 <unit id="org.slf4j.binding.simple" version="0.0.0"/>
 <repository location="https://download.eclipse.org/tools/orbit/downloads/drops/R20230531010532/repository/"/>
 </location>

2. Add the binding plug-in matching the target as an extra requirement in the profile (you may want to use a profile that only applies for SWTBot test plug-ins) in the pom.xml:

   <profile>
     <id>my-test-profile-id</id>
     <build>
       <plugins>
         <plugin>
           <groupId>org.eclipse.tycho</groupId>
           <artifactId>target-platform-configuration</artifactId>
           <configuration>
             <dependency-resolution>
               <extraRequirements>
                 <requirement>
                   <type>eclipse-plugin</type>
                   <id>org.slf4j.binding.simple</id>
                   <versionRange>1.7.30</versionRange>
                 </requirement>
               </extraRequirements>
             </dependency-resolution>
           </configuration>
         </plugin>
       </plugins>
     </build>
   </profile>

3. To configure the chosen logger, a properties file might need to be used, e.g. log4j.properties or simplelogger.properties. However this file is searched by slf4j by the class loader of the org.slf4j.api plug-in. In order to be found, this file should be included in a Fragment Project plug-in that has org.slf4j.api as a fragment host:

 Manifest-Version: 1.0
 Bundle-ManifestVersion: 2
 Bundle-Name: My Project SLF4J Binding Properties Fragment
 Bundle-SymbolicName: my.project.slf4j.binding.properties
 Bundle-Version: 1.0.0.qualifier
 Bundle-Vendor: My Vendor
 Fragment-Host: org.slf4j.api
 Automatic-Module-Name: my.project.slf4j.binding.properties
 Bundle-RequiredExecutionEnvironment: JavaSE-11

4. Create the desired properties file in the root of this fragment plug-in, for example simplelogger.properties:

 org.slf4j.simpleLogger.logFile=System.out
 org.slf4j.simpleLogger.defaultLogLevel=debug

5. Include the properties file in the build.properties of the fragment plug-in:

 bin.includes = META-INF/,\
                simplelogger.properties

6. Add the fragment plug-in as another extra dependency in the pom.xml, at the same level as the binding plug-in:

                 <requirement>
                   <type>eclipse-plugin</type>
                   <id>my.project.slf4j.binding.properties</id>
                   <versionRange>1.0.0</versionRange>
                 </requirement>

Back to the top