Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [cross-project-issues-dev] [swtbot-dev] Migration to SLF4J for SWTBot 4.0.0

Hi,

I have latest SWTBot along with simple, log4j and logback loggers (tested one at a time). Let me share the combinations that worked for me.

Install the required binder from update site e.g. SLF4J Simple Binding/ SLF4J log4j implemented over SLF4J and reload4j/ Logback SLF4J binding.

In all the below examples -

The versions of these jars may vary depending upon the eclipse version. Below mentioned versions are for eclipse 2021-12

It is important to pick the versions and jars compatible to your eclipse version from Orbit downloads.

‘lib’ folder in my examples below is the folder in the plugin that contains these jars.

 

  1. Simple-
  • MANIFEST.MF –

Import-Package: org.slf4j;version="1.7.2"

Bundle-ClassPath: .,

              lib/org.slf4j.api_1.7.2.v20121108-1250.jar,

        lib/org.slf4j.binding.simple_1.7.10.v20160301-1109.jar          

 

  • Test code-
Initialize logger as-

              private static final Logger LOG = LoggerFactory.getLogger(MyClass.class);


  • simplelogger.properties file at the root of plugin

 

  • PDE SWTBot Run launch-

Arguments tab can have level “-Dorg.slf4j.simpleLogger.defaultLogLevel=debug”

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

 

  1. Log4j-
  • MANIFEST.MF-

Require-Bundle: org.eclipse.swtbot.go,

 org.apache.log4j;bundle-version="1.2.15"

Import-Package: org.slf4j;version="1.7.2",

 org.slf4j.spi;version="1.7.2"

Bundle-ClassPath: .,

 lib/org.slf4j.impl.log4j12_1.7.2.v20131105-2200.jar

  • Test code Initialize log4j at start. E.g.

String log4jConfPath = "absolute_path\\log4j.properties"; where absolute_path is specific to your setup

PropertyConfigurator.configure(log4jConfPath);

  • log4j.properties at the root of the plugin
  • PDE SWTBot Run launch-

Plugins tab- Include org.slf4j.impl.log4j12 and org.slf4j.api along with other required plugins

Do not include too many similar jars of log4j; otherwise it will cause StackOverflow

As per https://stackoverflow.com/questions/52292078/log4j-stackoverflowerror ,The key point is that you cannot have the binding and the bridge for the same implementation on the classpath at the same time. Otherwise, you get what you see here: a stack overflow because the bridge is calling SLF4J which is calling the binding which is calling the bridge.

 

  1. Logback-

 

  • MANIFEST.MF-

Require-Bundle: org.eclipse.swtbot.go,

 ch.qos.logback.classic;bundle-version="1.1.2",

 ch.qos.logback.core;bundle-version="1.1.2"

Import-Package: org.slf4j;version="1.7.10"

Bundle-ClassPath: lib/ch.qos.logback.slf4j_1.1.2.v20160301-0943.jar,

 .

  • 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


Hope it helps to some extent. It is required to update swtbot wiki with slf4j migration.
Regards,
Aparna



On Wednesday, 1 June, 2022, 03:20:31 am IST, Patrick Tasse <patrick.tasse@xxxxxxxxx> wrote:


Hi everyone,

As part of the 2022-06 release, the SWTBot project will do a major release of version 4.0.0. In this version, SWTBot has removed its use of org.apache.log4j and is instead using the org.slf4j.api facade for its logging. See Bug 578065 for this change.

I am trying to adapt project Trace Compass to this change and I am having some issues, perhaps someone can help and/or this information will be useful to others.

Trace Compass was using the log4j Logger for its SWTBot tests, both to add its own messages and to rely on SWTBot's log4j debug messages.

The first issue when switching to SWTBot 4.0.0 is that the SWTBot update site no longer contains org.apache.log4j. So I first tried to change our target files to add org.apache.log4j from the Orbit update site. I also had to add an Import-Package dependency to org.slf4j in our swtbot.tests plug-ins MANIFEST.MF, to avoid 'missing ... indirectly referenced from .class files' errors, due to it now being used by SWTBot classes.

While the build was now successful, there was no debug logging from the SWTBot components because there was no slf4j binding installed. The console would show:

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

At this point, needing a quick fix, I changed our target files to use the previous SWTBot release update site (3.1.0) that still uses org.apache.log4j, instead of using the latest snapshot (4.0.0) update site.

So, now I am trying to do a proper migration to SLF4J.

I updated our targets to use the SWTBot 4.0.0 snapshot and added org.slf4j.api Import-Package dependency to the swtbot.tests plug-ins MANIFEST.MF. I removed all use of org.apache.log4j.Logger in our test code and replaced it with getting a logger from the org.slf4j.LoggerFactory.

Of course there was no logging when running the tests because there was no binding installed.

~~~ org.slf4j.binding.simple ~~~

I first tried to add org.slf4j.binding.simple from Orbit to our targets.

In my JUnit Plug-In Test Debug Configuration I can see that org.slf4j.api and org.slf4j.binding.simple plug-ins are added. When I ran the test it did not log anything because the default log level was INFO.

So I added this line as static initializer in my test code:

System.setProperty("org.slf4j.simpleLogger.log.org.eclipse", "debug");

This works because it covers both our test code classes org.eclipse.tracecompass.* and the SWTBot classes org.eclipse.swtbot.*. But it's a bit clunky, and I wanted to configure other settings, like setting the log file to System.out instead of System.err.

I tried creating a simplelogger.properties file in my test plug-in, adding it to the Binary Build in the Build Configuration tab and adding it's location to the Classpath in the Runtime tab.

But when I run the test, the SimpleLoggerConfiguration code tries to load the properties file from:
Thread.currentThread().getContextClassLoader() (java.lang)
The current thread is PluginTestRunner.
This returns a ContextFinder (org.eclipse.osgi)
But the ContextFinder.basicFindClassLoaders() implementation returns the first in the list that is a Bundle class loader, and that's the one at index [5] which is for org.slf4j.api bundle. So it never gets to index [19] which is our tmf.ui.swtbot.tests bundle that has the simplelogger.properties resource.

So it doesn't find our test plug-in's simplelogger.properties file and I can't configure the logger.

I don't really understand how class loaders work so at this point I gave up on SimpleLogger.

~~~ org.slf4j.binding.log4j12 ~~~

I secondly tried to add org.slf4j.binding.log4j12 from Orbit to our targets.

In my JUnit Plug-In Test Debug Configuration I can see that org.slf4j.api and org.slf4j.binding.log4j12 plug-ins are added.

But org.slf4j.binding.log4j12 requires org.apache.log4j package and this is found in org.slf4j.log4j, so this plug-in also needs to be added.

And now when I run my test, the org.apache.log4j.Log4jLoggerFactory static initializer checks that there is no org.slf4j.impl.Log4jLoggerFactory loaded, and since there is, it throws the following exception and aborts the test:

SLF4J: Detected both log4j-over-slf4j.jar AND bound slf4j-log4j12.jar on the class path, preempting StackOverflowError.
SLF4J: See also http://www.slf4j.org/codes.html#log4jDelegationLoop for more details.
Exception in thread "Plug-in Tests Runner" java.lang.ExceptionInInitializerError
at org.slf4j.impl.StaticLoggerBinder.<init>(StaticLoggerBinder.java:72)
Caused by: java.lang.IllegalStateException
at org.slf4j.impl.Log4jLoggerFactory.<clinit>(Log4jLoggerFactory.java:54)

It seems they are both mutually exclusive but I'm confused because org.apache.log4j is required by org.slf4j.binding.log4j12 ?!

~~~

So at this point I realize that I don't know what I'm doing wrong, and/or I don't know what I'm doing. Help?

I would like to figure this out so that we can help other projects that need to do a similar migration when they switch to SWTBot 4.0.0 after the upcoming release.

Thanks,

Patrick
SWTBot lead & Trace Compass committer
_______________________________________________
swtbot-dev mailing list
swtbot-dev@xxxxxxxxxxx
To unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/swtbot-dev

Back to the top