You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by pk...@apache.org on 2023/11/29 11:41:36 UTC

(logging-log4j2) branch main updated (5c4868a2f6 -> 72b2048bde)

This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a change to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


    from 5c4868a2f6 Improve build instructions
     new efee635999 Bump SLF4j to version 2.0.9
     new 72b2048bde Fix OSGi tests

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .github/dependabot.yaml                            |   4 -
 .../log4j/test/junit/TestPropertyResolver.java     |   4 +-
 .../logging/log4j/message/ThreadDumpMessage.java   |   3 +-
 log4j-core-its/pom.xml                             |   5 -
 log4j-core-test/pom.xml                            |   3 +
 .../log4j/core/config/AbstractConfiguration.java   |   4 +-
 .../logging/log4j/core/util/WatchManager.java      |   3 +-
 log4j-osgi-test/pom.xml                            |  81 +++++++---
 .../logging/log4j/osgi/tests/CoreOsgiTest.java     |   8 +
 .../logging/log4j/osgi/tests/JULProviderTest.java  |   7 +-
 .../log4j/osgi/tests/SLF4JProviderTest.java        |   6 +
 .../tests/equinox/EquinoxLoadApiBundleTest.java    |   2 +
 .../osgi/tests/felix/FelixLoadApiBundleTest.java   |   2 +
 log4j-parent/pom.xml                               |   8 +-
 log4j-perf-test/pom.xml                            |   4 -
 log4j-slf4j2-impl/pom.xml                          |  17 +--
 .../cloud/config/client/WatchEventManager.java     |   3 +-
 log4j-to-slf4j/pom.xml                             |   7 +-
 .../logging/slf4j/CallerInformationTest.java       |  13 +-
 .../apache/logging/slf4j/Log4j2Jira1688Test.java   |  10 +-
 .../org/apache/logging/slf4j/LogBuilderTest.java   |  15 +-
 .../logging/slf4j/LoggerContextResolver.java       | 166 +++++++++++++++++++++
 .../apache/logging/slf4j/LoggerContextRule.java    |  72 ---------
 .../apache/logging/slf4j/LoggerContextSource.java  |  27 +++-
 .../org/apache/logging/slf4j/LoggerResolver.java   |  84 +++++++++++
 .../java/org/apache/logging/slf4j/LoggerTest.java  |  31 ++--
 .../logging/slf4j/CallerInformationTest.xml}       |   0
 .../apache/logging/slf4j/LogBuilderTest.xml}       |   0
 .../apache/logging/slf4j/LoggerTest.xml}           |   0
 29 files changed, 412 insertions(+), 177 deletions(-)
 create mode 100644 log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextResolver.java
 delete mode 100644 log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextRule.java
 copy log4j-core-test/src/main/java/org/apache/logging/log4j/core/test/junit/ContextSelectorType.java => log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextSource.java (59%)
 create mode 100644 log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerResolver.java
 rename log4j-to-slf4j/src/test/resources/{logback-calling-class.xml => org/apache/logging/slf4j/CallerInformationTest.xml} (100%)
 rename log4j-to-slf4j/src/test/resources/{logback-turbofilter.xml => org/apache/logging/slf4j/LogBuilderTest.xml} (100%)
 rename log4j-to-slf4j/src/test/resources/{logback-slf4j.xml => org/apache/logging/slf4j/LoggerTest.xml} (100%)


(logging-log4j2) 02/02: Fix OSGi tests

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit 72b2048bde5cc84819b00f9c819292d0cfd8579e
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Mon Oct 30 13:08:47 2023 +0100

    Fix OSGi tests
---
 .../logging/log4j/message/ThreadDumpMessage.java   |  3 +-
 .../log4j/core/config/AbstractConfiguration.java   |  4 +-
 .../logging/log4j/core/util/WatchManager.java      |  3 +-
 log4j-osgi-test/pom.xml                            | 81 +++++++++++++++-------
 .../logging/log4j/osgi/tests/CoreOsgiTest.java     |  8 +++
 .../logging/log4j/osgi/tests/JULProviderTest.java  |  7 +-
 .../log4j/osgi/tests/SLF4JProviderTest.java        |  6 ++
 .../tests/equinox/EquinoxLoadApiBundleTest.java    |  2 +
 .../osgi/tests/felix/FelixLoadApiBundleTest.java   |  2 +
 log4j-parent/pom.xml                               |  2 +-
 .../cloud/config/client/WatchEventManager.java     |  3 +-
 log4j-to-slf4j/pom.xml                             |  2 -
 12 files changed, 90 insertions(+), 33 deletions(-)

diff --git a/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java b/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java
index 23314772b2..b0d699f69c 100644
--- a/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java
+++ b/log4j-api/src/main/java/org/apache/logging/log4j/message/ThreadDumpMessage.java
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.message;
 
 import aQute.bnd.annotation.Cardinality;
+import aQute.bnd.annotation.Resolution;
 import aQute.bnd.annotation.spi.ServiceConsumer;
 import java.util.HashMap;
 import java.util.Map;
@@ -31,7 +32,7 @@ import org.apache.logging.log4j.util.Strings;
  * Captures information about all running Threads.
  */
 @AsynchronouslyFormattable
-@ServiceConsumer(value = ThreadInfoFactory.class, cardinality = Cardinality.SINGLE)
+@ServiceConsumer(value = ThreadInfoFactory.class, cardinality = Cardinality.SINGLE, resolution = Resolution.OPTIONAL)
 public class ThreadDumpMessage implements Message, StringBuilderFormattable {
     private static final Lazy<ThreadInfoFactory> FACTORY = Lazy.lazy(() -> ServiceLoaderUtil.safeStream(
                     ServiceLoader.load(ThreadInfoFactory.class, ThreadInfoFactory.class.getClassLoader()))
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
index d700c67dee..631a8eabf9 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java
@@ -16,6 +16,8 @@
  */
 package org.apache.logging.log4j.core.config;
 
+import aQute.bnd.annotation.Cardinality;
+import aQute.bnd.annotation.Resolution;
 import aQute.bnd.annotation.spi.ServiceConsumer;
 import java.lang.ref.WeakReference;
 import java.util.ArrayList;
@@ -91,7 +93,7 @@ import org.apache.logging.log4j.util.ServiceLoaderUtil;
 /**
  * The base Configuration. Many configuration implementations will extend this class.
  */
-@ServiceConsumer(ScriptManagerFactory.class)
+@ServiceConsumer(value = ScriptManagerFactory.class, cardinality = Cardinality.SINGLE, resolution = Resolution.OPTIONAL)
 public abstract class AbstractConfiguration extends AbstractFilterable implements Configuration {
 
     /**
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/WatchManager.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/WatchManager.java
index ac826ebd07..6df54530bd 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/WatchManager.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/WatchManager.java
@@ -17,6 +17,7 @@
 package org.apache.logging.log4j.core.util;
 
 import aQute.bnd.annotation.Cardinality;
+import aQute.bnd.annotation.Resolution;
 import aQute.bnd.annotation.spi.ServiceConsumer;
 import java.io.File;
 import java.util.Date;
@@ -47,7 +48,7 @@ import org.apache.logging.log4j.util.ServiceLoaderUtil;
  * @see ConfigurationScheduler
  */
 @Singleton
-@ServiceConsumer(value = WatchEventService.class, cardinality = Cardinality.MULTIPLE)
+@ServiceConsumer(value = WatchEventService.class, cardinality = Cardinality.MULTIPLE, resolution = Resolution.OPTIONAL)
 public class WatchManager extends AbstractLifeCycle {
 
     private static final class ConfigurationMonitor {
diff --git a/log4j-osgi-test/pom.xml b/log4j-osgi-test/pom.xml
index 34bb227181..d35eb1ee1a 100644
--- a/log4j-osgi-test/pom.xml
+++ b/log4j-osgi-test/pom.xml
@@ -23,20 +23,25 @@
     <version>${revision}</version>
     <relativePath>../log4j-parent</relativePath>
   </parent>
-
   <artifactId>log4j-osgi-test</artifactId>
   <packaging>jar</packaging>
-  <name>Apache Log4j OSGi</name>
+  <name>Apache Log4j OSGi tests</name>
   <description>The Apache Log4j OSGi tests</description>
   <properties>
-    <log4jParentDir>${basedir}/..</log4jParentDir>
+    <docLabel>OSGi Documentation</docLabel>
+    <projectDir>/osgi</projectDir>
     <module.name>org.apache.logging.log4j.osgi</module.name>
-    <skipTests>true</skipTests>
     <bnd.baseline.skip>true</bnd.baseline.skip>
     <maven.deploy.skip>true</maven.deploy.skip>
     <maven.install.skip>true</maven.install.skip>
+    <spifly.version>1.3.6</spifly.version>
   </properties>
   <dependencies>
+    <dependency>
+      <groupId>javax.inject</groupId>
+      <artifactId>javax.inject</artifactId>
+      <version>1</version>
+    </dependency>
     <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-1.2-api</artifactId>
@@ -47,6 +52,11 @@
       <artifactId>log4j-api</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-api-test</artifactId>
+      <scope>test</scope>
+    </dependency>
     <dependency>
       <groupId>org.apache.logging.log4j</groupId>
       <artifactId>log4j-core</artifactId>
@@ -67,11 +77,6 @@
       <artifactId>log4j-to-slf4j</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.hamcrest</groupId>
-      <artifactId>hamcrest</artifactId>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>org.junit.jupiter</groupId>
       <artifactId>junit-jupiter-engine</artifactId>
@@ -88,20 +93,11 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>ch.qos.logback</groupId>
-      <artifactId>logback-core</artifactId>
-      <scope>test</scope>
-    </dependency>
-    <dependency>
-      <groupId>org.osgi</groupId>
-      <artifactId>org.osgi.framework</artifactId>
-    </dependency>
-    <dependency>
-      <groupId>org.apache.maven</groupId>
-      <artifactId>maven-core</artifactId>
+      <groupId>org.apache.aries.spifly</groupId>
+      <artifactId>org.apache.aries.spifly.dynamic.bundle</artifactId>
+      <version>${spifly.version}</version>
       <scope>test</scope>
     </dependency>
-    <!-- Place Felix before Equinox because Felix is signed. / also place it before org.osgi.core so that its versions of the OSGi classes are used -->
     <dependency>
       <groupId>org.apache.felix</groupId>
       <artifactId>org.apache.felix.framework</artifactId>
@@ -140,6 +136,29 @@
   </dependencies>
   <build>
     <plugins>
+
+      <!--
+        ~ Unban Logback.
+        -->
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-enforcer-plugin</artifactId>
+        <executions>
+          <execution>
+            <id>ban-logging-dependencies</id>
+            <configuration>
+              <rules>
+                <bannedDependencies>
+                  <includes>
+                    <include>ch.qos.logback:*:*:*:test</include>
+                  </includes>
+                </bannedDependencies>
+              </rules>
+            </configuration>
+          </execution>
+        </executions>
+      </plugin>
+
       <plugin>
         <groupId>org.ops4j.pax.exam</groupId>
         <artifactId>exam-maven-plugin</artifactId>
@@ -152,13 +171,26 @@
           </execution>
         </executions>
       </plugin>
+
+      <plugin>
+        <groupId>org.apache.maven.plugins</groupId>
+        <artifactId>maven-compiler-plugin</artifactId>
+        <configuration>
+          <annotationProcessorPaths combine.children="append">
+            <path>
+              <groupId>org.apache.logging.log4j</groupId>
+              <artifactId>log4j-plugin-processor</artifactId>
+              <version>${project.version}</version>
+            </path>
+          </annotationProcessorPaths>
+        </configuration>
+      </plugin>
+
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
         <configuration>
-          <classpathDependencyExcludes>
-            <classpathDependencyExclude>org.osgi:org.osgi.framework</classpathDependencyExclude>
-          </classpathDependencyExcludes>
+          <classpathDependencyExcludes>org.osgi:org.osgi.framework</classpathDependencyExcludes>
           <systemPropertyVariables>
             <!-- PAX logging has a copy of Log4j2 API-->
             <pax.exam.logging>false</pax.exam.logging>
@@ -166,6 +198,7 @@
           </systemPropertyVariables>
         </configuration>
       </plugin>
+
     </plugins>
   </build>
 </project>
diff --git a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CoreOsgiTest.java b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CoreOsgiTest.java
index 4dfb1c35da..08049c0dc9 100644
--- a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CoreOsgiTest.java
+++ b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/CoreOsgiTest.java
@@ -29,6 +29,7 @@ import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.config.Configuration;
 import org.apache.logging.log4j.core.impl.Log4jContextFactory;
 import org.apache.logging.log4j.spi.LoggerContextFactory;
+import org.junit.Ignore;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.ops4j.pax.exam.Option;
@@ -38,6 +39,7 @@ import org.ops4j.pax.exam.spi.reactors.PerClass;
 
 @RunWith(PaxExam.class)
 @ExamReactorStrategy(PerClass.class)
+@Ignore
 public class CoreOsgiTest {
 
     @org.ops4j.pax.exam.Configuration
@@ -48,6 +50,12 @@ public class CoreOsgiTest {
                 linkBundle("org.apache.logging.log4j.core"),
                 linkBundle("org.apache.logging.log4j.1.2.api").start(false),
                 // required by Pax Exam's logging
+                linkBundle("org.objectweb.asm"),
+                linkBundle("org.objectweb.asm.commons"),
+                linkBundle("org.objectweb.asm.tree"),
+                linkBundle("org.objectweb.asm.tree.analysis"),
+                linkBundle("org.objectweb.asm.util"),
+                linkBundle("org.apache.aries.spifly.dynamic.bundle").startLevel(2),
                 linkBundle("slf4j.api"),
                 linkBundle("ch.qos.logback.classic"),
                 linkBundle("ch.qos.logback.core"),
diff --git a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/JULProviderTest.java b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/JULProviderTest.java
index f827ae727e..3750222170 100644
--- a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/JULProviderTest.java
+++ b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/JULProviderTest.java
@@ -50,7 +50,12 @@ public class JULProviderTest {
         return options(
                 linkBundle("org.apache.logging.log4j.api"),
                 linkBundle("org.apache.logging.log4j.to.jul"),
-                // required by Pax Exam's logging
+                linkBundle("org.objectweb.asm"),
+                linkBundle("org.objectweb.asm.commons"),
+                linkBundle("org.objectweb.asm.tree"),
+                linkBundle("org.objectweb.asm.tree.analysis"),
+                linkBundle("org.objectweb.asm.util"),
+                linkBundle("org.apache.aries.spifly.dynamic.bundle").startLevel(2),
                 linkBundle("slf4j.api"),
                 linkBundle("ch.qos.logback.classic"),
                 linkBundle("ch.qos.logback.core"),
diff --git a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/SLF4JProviderTest.java b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/SLF4JProviderTest.java
index 5d9548ac69..cb0e1771fb 100644
--- a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/SLF4JProviderTest.java
+++ b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/SLF4JProviderTest.java
@@ -50,6 +50,12 @@ public class SLF4JProviderTest {
         return options(
                 linkBundle("org.apache.logging.log4j.api"),
                 linkBundle("org.apache.logging.log4j.to.slf4j"),
+                linkBundle("org.objectweb.asm"),
+                linkBundle("org.objectweb.asm.commons"),
+                linkBundle("org.objectweb.asm.tree"),
+                linkBundle("org.objectweb.asm.tree.analysis"),
+                linkBundle("org.objectweb.asm.util"),
+                linkBundle("org.apache.aries.spifly.dynamic.bundle").startLevel(2),
                 linkBundle("slf4j.api"),
                 linkBundle("ch.qos.logback.classic"),
                 linkBundle("ch.qos.logback.core"),
diff --git a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/equinox/EquinoxLoadApiBundleTest.java b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/equinox/EquinoxLoadApiBundleTest.java
index b373650a03..6cc5389b01 100644
--- a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/equinox/EquinoxLoadApiBundleTest.java
+++ b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/equinox/EquinoxLoadApiBundleTest.java
@@ -18,11 +18,13 @@ package org.apache.logging.log4j.osgi.tests.equinox;
 
 import org.apache.logging.log4j.osgi.tests.AbstractLoadBundleTest;
 import org.eclipse.osgi.launch.EquinoxFactory;
+import org.junit.Ignore;
 import org.osgi.framework.launch.FrameworkFactory;
 
 /**
  * Tests loading the Core bundle into an Eclipse Equinox OSGi container.
  */
+@Ignore
 public class EquinoxLoadApiBundleTest extends AbstractLoadBundleTest {
 
     @Override
diff --git a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/felix/FelixLoadApiBundleTest.java b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/felix/FelixLoadApiBundleTest.java
index 59f29302ea..f301caac96 100644
--- a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/felix/FelixLoadApiBundleTest.java
+++ b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/felix/FelixLoadApiBundleTest.java
@@ -17,11 +17,13 @@
 package org.apache.logging.log4j.osgi.tests.felix;
 
 import org.apache.logging.log4j.osgi.tests.AbstractLoadBundleTest;
+import org.junit.Ignore;
 import org.osgi.framework.launch.FrameworkFactory;
 
 /**
  * Tests loading the Core bundle into an Apache Felix OSGi container.
  */
+@Ignore
 public class FelixLoadApiBundleTest extends AbstractLoadBundleTest {
 
     @Override
diff --git a/log4j-parent/pom.xml b/log4j-parent/pom.xml
index 7597c263b6..6324588a02 100644
--- a/log4j-parent/pom.xml
+++ b/log4j-parent/pom.xml
@@ -141,7 +141,7 @@
     <log4j2-custom-layout.version>1.1.0</log4j2-custom-layout.version>
     <log4j2-ecs-layout.version>1.5.0</log4j2-ecs-layout.version>
     <log4j2-logstash-layout.version>0.18</log4j2-logstash-layout.version>
-    <logback.version>1.4.11</logback.version>
+    <logback.version>1.3.11</logback.version>
     <maven.version>3.8.6</maven.version>
     <mockito.version>5.7.0</mockito.version>
     <mongodb.version>4.11.1</mongodb.version>
diff --git a/log4j-spring-cloud-config-client/src/main/java/org/apache/logging/log4j/spring/cloud/config/client/WatchEventManager.java b/log4j-spring-cloud-config-client/src/main/java/org/apache/logging/log4j/spring/cloud/config/client/WatchEventManager.java
index 44a7b90b25..3c8c0f7167 100644
--- a/log4j-spring-cloud-config-client/src/main/java/org/apache/logging/log4j/spring/cloud/config/client/WatchEventManager.java
+++ b/log4j-spring-cloud-config-client/src/main/java/org/apache/logging/log4j/spring/cloud/config/client/WatchEventManager.java
@@ -16,7 +16,6 @@
  */
 package org.apache.logging.log4j.spring.cloud.config.client;
 
-import aQute.bnd.annotation.Resolution;
 import aQute.bnd.annotation.spi.ServiceProvider;
 import java.util.UUID;
 import java.util.concurrent.ConcurrentHashMap;
@@ -24,7 +23,7 @@ import java.util.concurrent.ConcurrentMap;
 import org.apache.logging.log4j.core.util.WatchEventService;
 import org.apache.logging.log4j.core.util.WatchManager;
 
-@ServiceProvider(value = WatchEventService.class, resolution = Resolution.OPTIONAL)
+@ServiceProvider(value = WatchEventService.class)
 public class WatchEventManager implements WatchEventService {
     private static final ConcurrentMap<UUID, WatchManager> watchManagers = new ConcurrentHashMap<>();
 
diff --git a/log4j-to-slf4j/pom.xml b/log4j-to-slf4j/pom.xml
index 51b840b076..5996949f0e 100644
--- a/log4j-to-slf4j/pom.xml
+++ b/log4j-to-slf4j/pom.xml
@@ -23,13 +23,11 @@
     <version>${revision}</version>
     <relativePath>../log4j-parent</relativePath>
   </parent>
-
   <artifactId>log4j-to-slf4j</artifactId>
   <packaging>jar</packaging>
   <name>Apache Log4j to SLF4J Adapter</name>
   <description>The Apache Log4j binding between Log4j 2 API and SLF4J.</description>
   <properties>
-    <log4jParentDir>${basedir}/..</log4jParentDir>
 
     <!--
       ~ OSGi and JPMS options


(logging-log4j2) 01/02: Bump SLF4j to version 2.0.9

Posted by pk...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit efee635999e2d96ca8d5fb2c0ad3c42a6af4e8f2
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Mon Oct 30 11:21:52 2023 +0100

    Bump SLF4j to version 2.0.9
    
    Most of our modules with a transitive dependency on `slf4j-api` used
    version 1.7.36, while `log4j-slf4j-impl` used version 1.7.25 (due to a
    breaking change in `slf4j-ext`) and `log4j-slf4j2-impl` used version
    2.0.x.
    
    This PR simplifies this configuration by:
     * switching `log4j-slf4j-impl` to use version 1.7.36 of `slf4j-api` and
       version 1.7.25 of `slf4j-ext`,
     * removing the need of `slf4j-ext` where applicable,
     * switching all the other modules to SLF4J 2.x,
     * modernizing the `log4j-to-slf4j` tests to use JUnit 5 instead of
       JUnit 4.
---
 .github/dependabot.yaml                            |   4 -
 .../log4j/test/junit/TestPropertyResolver.java     |   4 +-
 log4j-core-its/pom.xml                             |   5 -
 log4j-core-test/pom.xml                            |   3 +
 log4j-parent/pom.xml                               |   8 +-
 log4j-perf-test/pom.xml                            |   4 -
 log4j-slf4j2-impl/pom.xml                          |  17 +--
 log4j-to-slf4j/pom.xml                             |   5 +
 .../logging/slf4j/CallerInformationTest.java       |  13 +-
 .../apache/logging/slf4j/Log4j2Jira1688Test.java   |  10 +-
 .../org/apache/logging/slf4j/LogBuilderTest.java   |  15 +-
 .../logging/slf4j/LoggerContextResolver.java       | 166 +++++++++++++++++++++
 .../apache/logging/slf4j/LoggerContextRule.java    |  72 ---------
 .../apache/logging/slf4j/LoggerContextSource.java  |  52 +++++++
 .../org/apache/logging/slf4j/LoggerResolver.java   |  84 +++++++++++
 .../java/org/apache/logging/slf4j/LoggerTest.java  |  31 ++--
 .../logging/slf4j/CallerInformationTest.xml}       |   0
 .../apache/logging/slf4j/LogBuilderTest.xml}       |   0
 .../apache/logging/slf4j/LoggerTest.xml}           |   0
 19 files changed, 356 insertions(+), 137 deletions(-)

diff --git a/.github/dependabot.yaml b/.github/dependabot.yaml
index 38c9502f2e..0869fcb5ce 100644
--- a/.github/dependabot.yaml
+++ b/.github/dependabot.yaml
@@ -40,8 +40,6 @@ updates:
     # Tomcat Juli 10.1.x requires Java 11
     - dependency-name: "org.apache.tomcat:*"
       update-types: ["version-update:semver-major", "version-update:semver-minor"]
-    # Always update SLF4J manually
-    - dependency-name: "org.slf4j:*"
     # Keep Logback version 1.2.x
     - dependency-name: "ch.qos.logback:*"
       update-types: ["version-update:semver-major", "version-update:semver-minor"]
@@ -110,8 +108,6 @@ updates:
     # Spring Cloud 2022.x is Jakarta EE 9
     - dependency-name: "org.springframework.cloud:*"
       update-types: ["version-update:semver-major"]
-    # Always update SLF4J manually
-    - dependency-name: "org.slf4j:*"
     # Keep Logback version 1.2.x
     - dependency-name: "ch.qos.logback:*"
       update-types: ["version-update:semver-major", "version-update:semver-minor"]
diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TestPropertyResolver.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TestPropertyResolver.java
index 0fe795aa41..b86a6d711c 100644
--- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TestPropertyResolver.java
+++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TestPropertyResolver.java
@@ -45,7 +45,7 @@ public class TestPropertyResolver extends TypeBasedParameterResolver<TestPropert
         ReflectionSupport.findFields(
                         testClass,
                         field -> ModifierSupport.isNotStatic(field)
-                                && field.getType().isAssignableFrom(TestProperties.class),
+                                && field.getType().equals(TestProperties.class),
                         HierarchyTraversalMode.BOTTOM_UP)
                 .forEach(field -> ReflectionUtil.setFieldValue(field, testInstance, props));
     }
@@ -59,7 +59,7 @@ public class TestPropertyResolver extends TypeBasedParameterResolver<TestPropert
         ReflectionSupport.findFields(
                         testClass,
                         field -> ModifierSupport.isStatic(field)
-                                && field.getType().isAssignableFrom(TestProperties.class),
+                                && field.getType().equals(TestProperties.class),
                         HierarchyTraversalMode.BOTTOM_UP)
                 .forEach(field -> ReflectionUtil.setStaticFieldValue(field, props));
     }
diff --git a/log4j-core-its/pom.xml b/log4j-core-its/pom.xml
index 46edbf02b8..4a6cb8f85d 100644
--- a/log4j-core-its/pom.xml
+++ b/log4j-core-its/pom.xml
@@ -152,11 +152,6 @@
       <artifactId>slf4j-api</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-ext</artifactId>
-      <scope>test</scope>
-    </dependency>
     <!-- Used for testing HttpAppender -->
     <dependency>
       <groupId>com.github.tomakehurst</groupId>
diff --git a/log4j-core-test/pom.xml b/log4j-core-test/pom.xml
index fde90d2614..d111d8dfbe 100644
--- a/log4j-core-test/pom.xml
+++ b/log4j-core-test/pom.xml
@@ -35,6 +35,9 @@
     <!-- Dependency versions -->
     <slf4j-ext.version>${slf4j.version}</slf4j-ext.version>
 
+    <!-- Dependency versions -->
+    <slf4j-ext.version>${slf4j.version}</slf4j-ext.version>
+
     <!--
       ~ OSGi and JPMS options
       -->
diff --git a/log4j-parent/pom.xml b/log4j-parent/pom.xml
index 018d7829a6..7597c263b6 100644
--- a/log4j-parent/pom.xml
+++ b/log4j-parent/pom.xml
@@ -141,7 +141,7 @@
     <log4j2-custom-layout.version>1.1.0</log4j2-custom-layout.version>
     <log4j2-ecs-layout.version>1.5.0</log4j2-ecs-layout.version>
     <log4j2-logstash-layout.version>0.18</log4j2-logstash-layout.version>
-    <logback.version>1.2.12</logback.version>
+    <logback.version>1.4.11</logback.version>
     <maven.version>3.8.6</maven.version>
     <mockito.version>5.7.0</mockito.version>
     <mongodb.version>4.11.1</mongodb.version>
@@ -155,7 +155,9 @@
     <osgi.resource.version>1.0.1</osgi.resource.version>
     <pax-exam.version>4.13.5</pax-exam.version>
     <plexus-utils.version>3.5.0</plexus-utils.version>
-    <slf4j.version>1.7.36</slf4j.version>
+    <slf4j.version>2.0.9</slf4j.version>
+    <!-- Last version before a breaking change -->
+    <slf4j-ext.version>1.7.25</slf4j-ext.version>
     <spring-framework.version>5.3.31</spring-framework.version>
     <system-stubs.version>2.1.5</system-stubs.version>
     <tomcat-juli.version>10.0.23</tomcat-juli.version>
@@ -820,7 +822,7 @@
       <dependency>
         <groupId>org.slf4j</groupId>
         <artifactId>slf4j-ext</artifactId>
-        <version>${slf4j.version}</version>
+        <version>${slf4j-ext.version}</version>
       </dependency>
 
       <dependency>
diff --git a/log4j-perf-test/pom.xml b/log4j-perf-test/pom.xml
index bdb1f9f19c..d00fbf0a24 100644
--- a/log4j-perf-test/pom.xml
+++ b/log4j-perf-test/pom.xml
@@ -126,10 +126,6 @@
       <groupId>org.slf4j</groupId>
       <artifactId>slf4j-api</artifactId>
     </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-ext</artifactId>
-    </dependency>
   </dependencies>
 
   <build>
diff --git a/log4j-slf4j2-impl/pom.xml b/log4j-slf4j2-impl/pom.xml
index cba8d4610f..568f9cf005 100644
--- a/log4j-slf4j2-impl/pom.xml
+++ b/log4j-slf4j2-impl/pom.xml
@@ -23,15 +23,11 @@
     <version>${revision}</version>
     <relativePath>../log4j-parent</relativePath>
   </parent>
-
   <artifactId>log4j-slf4j2-impl</artifactId>
   <packaging>jar</packaging>
   <name>Apache Log4j SLF4J 2.0 Binding</name>
   <description>The Apache Log4j SLF4J 2.0 API binding to Log4j 2 Core</description>
   <properties>
-    <log4jParentDir>${basedir}/..</log4jParentDir>
-    <!-- Override parent's default -->
-    <slf4j.version>2.0.6</slf4j.version>
 
     <!--
       ~ OSGi and JPMS options
@@ -76,11 +72,6 @@
       <artifactId>log4j-to-slf4j</artifactId>
       <scope>test</scope>
     </dependency>
-    <dependency>
-      <groupId>org.slf4j</groupId>
-      <artifactId>slf4j-ext</artifactId>
-      <scope>test</scope>
-    </dependency>
     <dependency>
       <groupId>org.assertj</groupId>
       <artifactId>assertj-core</artifactId>
@@ -111,9 +102,16 @@
       <artifactId>junit-vintage-engine</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.slf4j</groupId>
+      <artifactId>slf4j-ext</artifactId>
+      <version>${slf4j.version}</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
+
       <plugin>
         <groupId>org.apache.maven.plugins</groupId>
         <artifactId>maven-surefire-plugin</artifactId>
@@ -151,6 +149,7 @@
           </execution>
         </executions>
       </plugin>
+
     </plugins>
   </build>
 </project>
diff --git a/log4j-to-slf4j/pom.xml b/log4j-to-slf4j/pom.xml
index c0334118ae..51b840b076 100644
--- a/log4j-to-slf4j/pom.xml
+++ b/log4j-to-slf4j/pom.xml
@@ -90,6 +90,11 @@
       <type>test-jar</type>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.logging.log4j</groupId>
+      <artifactId>log4j-api-test</artifactId>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
 
 </project>
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java
index 50f7deb940..b43a4ea68f 100644
--- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/CallerInformationTest.java
@@ -16,22 +16,19 @@
  */
 package org.apache.logging.slf4j;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
 
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.testUtil.StringListAppender;
 import java.util.List;
 import org.apache.logging.log4j.LogManager;
-import org.junit.ClassRule;
-import org.junit.Test;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
 
+@UsingStatusListener
+@LoggerContextSource
 public class CallerInformationTest {
 
-    private static final String CONFIG = "target/test-classes/logback-calling-class.xml";
-
-    @ClassRule
-    public static final LoggerContextRule CTX = new LoggerContextRule(CONFIG);
-
     @Test
     public void testClassLogger() throws Exception {
         final SLF4JLogger logger = (SLF4JLogger) LogManager.getLogger("ClassLogger");
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/Log4j2Jira1688Test.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/Log4j2Jira1688Test.java
index d288165b07..18779b11ee 100644
--- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/Log4j2Jira1688Test.java
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/Log4j2Jira1688Test.java
@@ -16,9 +16,10 @@
  */
 package org.apache.logging.slf4j;
 
+import static org.assertj.core.api.Assertions.assertThat;
+
 import java.util.Arrays;
-import org.junit.Assert;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -44,14 +45,13 @@ public class Log4j2Jira1688Test {
         // First logging of args
         logger.error(someFormat, args); // Only the first element (args[0]) of args will be logged - why?
         // GG: because the pattern {} picks up the 1 st argument, not the whole array
-        Assert.assertArrayEquals(originalArgs, args);
-        System.out.println("args are still ok: " + Arrays.toString(args));
+        assertThat(args).containsExactly(originalArgs);
 
         // Bug: The second logging of args sets all elements of args to null
         logger.error(someFormat, args);
         // GG: All is well args is still intact
         System.out.println("args " + Arrays.toString(args));
-        Assert.assertArrayEquals(originalArgs, args);
+        assertThat(args).containsExactly(originalArgs);
     }
 
     /**
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LogBuilderTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LogBuilderTest.java
index a9bcba97c3..8d073072b9 100644
--- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LogBuilderTest.java
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LogBuilderTest.java
@@ -19,7 +19,6 @@ package org.apache.logging.slf4j;
 import static org.assertj.core.api.Assertions.assertThat;
 
 import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.joran.JoranConfigurator;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.testUtil.StringListAppender;
 import java.util.function.Consumer;
@@ -31,31 +30,29 @@ import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.message.Message;
 import org.apache.logging.log4j.message.SimpleMessage;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
 import org.junit.jupiter.api.BeforeAll;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.MethodSource;
-import org.slf4j.LoggerFactory;
 
+@UsingStatusListener
+@LoggerContextSource
 public class LogBuilderTest {
 
-    private static final String CONFIG = "target/test-classes/logback-turbofilter.xml";
     private static final CharSequence CHAR_SEQUENCE = "CharSequence";
     private static final String STRING = "String";
     private static final Message MESSAGE = new SimpleMessage();
     private static final Object[] P = {"p0", "p1", "p2", "p3", "p4", "p5", "p6", "p7", "p8", "p9"};
     private static final Object OBJECT = "Object";
 
-    private static LoggerContext context;
+    // Log4j objects
     private static Logger logger;
+    // Logback objects
+    private static LoggerContext context;
     private static StringListAppender<ILoggingEvent> list;
 
     @BeforeAll
     public static void setUp() throws Exception {
-        context = (LoggerContext) LoggerFactory.getILoggerFactory();
-        final JoranConfigurator configurator = new JoranConfigurator();
-        configurator.setContext(context);
-        configurator.doConfigure(CONFIG);
-
         final org.slf4j.Logger slf4jLogger = context.getLogger(LogBuilderTest.class);
         logger = LogManager.getLogger(LogBuilderTest.class);
         assertThat(slf4jLogger).isSameAs(((SLF4JLogger) logger).getLogger());
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextResolver.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextResolver.java
new file mode 100644
index 0000000000..dc9dd5d343
--- /dev/null
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextResolver.java
@@ -0,0 +1,166 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.slf4j;
+
+import ch.qos.logback.classic.Logger;
+import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.joran.JoranConfigurator;
+import ch.qos.logback.core.joran.spi.JoranException;
+import java.net.URL;
+import org.apache.logging.log4j.test.junit.ExtensionContextAnchor;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ExtensionContext.Store;
+import org.junit.jupiter.api.extension.ExtensionContextException;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.support.TypeBasedParameterResolver;
+import org.junit.platform.commons.support.AnnotationSupport;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+import org.junit.platform.commons.support.ModifierSupport;
+import org.junit.platform.commons.support.ReflectionSupport;
+import org.slf4j.LoggerFactory;
+
+class LoggerContextResolver extends TypeBasedParameterResolver<LoggerContext>
+        implements BeforeAllCallback, BeforeEachCallback {
+
+    private static final Object KEY = LoggerContextHolder.class;
+
+    @Override
+    public void beforeEach(ExtensionContext extensionContext) throws Exception {
+        final Class<?> testClass = extensionContext.getRequiredTestClass();
+        if (AnnotationSupport.isAnnotated(testClass, LoggerContextSource.class)) {
+            final LoggerContextHolder holder =
+                    ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext);
+            if (holder == null) {
+                throw new IllegalStateException(
+                        "Specified @LoggerContextSource but no LoggerContext found for test class "
+                                + testClass.getCanonicalName());
+            }
+        }
+        AnnotationSupport.findAnnotation(extensionContext.getRequiredTestMethod(), LoggerContextSource.class)
+                .ifPresent(source -> {
+                    final LoggerContextHolder holder = new LoggerContextHolder(source, extensionContext);
+                    ExtensionContextAnchor.setAttribute(KEY, holder, extensionContext);
+                });
+        final LoggerContextHolder holder =
+                ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext);
+        if (holder != null) {
+            ReflectionSupport.findFields(
+                            extensionContext.getRequiredTestClass(),
+                            f -> ModifierSupport.isNotStatic(f) && f.getType().equals(LoggerContext.class),
+                            HierarchyTraversalMode.TOP_DOWN)
+                    .forEach(f -> {
+                        try {
+                            f.setAccessible(true);
+                            f.set(extensionContext.getRequiredTestInstance(), holder.getLoggerContext());
+                        } catch (ReflectiveOperationException e) {
+                            throw new ExtensionContextException("Failed to inject field " + f, e);
+                        }
+                    });
+        }
+    }
+
+    @Override
+    public void beforeAll(ExtensionContext extensionContext) throws Exception {
+        final Class<?> testClass = extensionContext.getRequiredTestClass();
+        AnnotationSupport.findAnnotation(testClass, LoggerContextSource.class).ifPresent(testSource -> {
+            final LoggerContextHolder holder = new LoggerContextHolder(testSource, extensionContext);
+            ExtensionContextAnchor.setAttribute(KEY, holder, extensionContext);
+        });
+        final LoggerContextHolder holder =
+                ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext);
+        if (holder != null) {
+            ReflectionSupport.findFields(
+                            extensionContext.getRequiredTestClass(),
+                            f -> ModifierSupport.isStatic(f) && f.getType().equals(LoggerContext.class),
+                            HierarchyTraversalMode.TOP_DOWN)
+                    .forEach(f -> {
+                        try {
+                            f.setAccessible(true);
+                            f.set(null, holder.getLoggerContext());
+                        } catch (ReflectiveOperationException e) {
+                            throw new ExtensionContextException("Failed to inject field " + f, e);
+                        }
+                    });
+        }
+    }
+
+    @Override
+    public LoggerContext resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
+            throws ParameterResolutionException {
+        return ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext)
+                .getLoggerContext();
+    }
+
+    static final class LoggerContextHolder implements Store.CloseableResource {
+
+        private final LoggerContext context;
+        private final Logger logger;
+
+        private LoggerContextHolder(final LoggerContextSource source, final ExtensionContext extensionContext) {
+            this.context = (LoggerContext) LoggerFactory.getILoggerFactory();
+            Class<?> clazz = extensionContext.getRequiredTestClass();
+            this.logger = context.getLogger(clazz);
+
+            final JoranConfigurator configurator = new JoranConfigurator();
+            final URL configLocation = getConfigLocation(source, extensionContext);
+            configurator.setContext(context);
+            try {
+                configurator.doConfigure(configLocation);
+            } catch (final JoranException e) {
+                throw new ExtensionContextException("Failed to initialize Logback logger context for " + clazz, e);
+            }
+        }
+
+        private static URL getConfigLocation(
+                final LoggerContextSource source, final ExtensionContext extensionContext) {
+            final String value = source.value();
+            Class<?> clazz = extensionContext.getRequiredTestClass();
+            URL url = null;
+            if (value.isEmpty()) {
+                while (clazz != null) {
+                    url = clazz.getResource(clazz.getSimpleName() + ".xml");
+                    if (url != null) {
+                        break;
+                    }
+                    clazz = clazz.getSuperclass();
+                }
+            } else {
+                url = clazz.getClassLoader().getResource(value);
+            }
+            if (url != null) {
+                return url;
+            }
+            throw new ExtensionContextException("Failed to find a default configuration for " + clazz);
+        }
+
+        public LoggerContext getLoggerContext() {
+            return context;
+        }
+
+        public Logger getLogger() {
+            return logger;
+        }
+
+        @Override
+        public void close() {
+            context.stop();
+        }
+    }
+}
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextRule.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextRule.java
deleted file mode 100644
index 86979313d4..0000000000
--- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextRule.java
+++ /dev/null
@@ -1,72 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to you 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
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.logging.slf4j;
-
-import ch.qos.logback.classic.Logger;
-import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.joran.JoranConfigurator;
-import ch.qos.logback.core.joran.GenericConfigurator;
-import org.junit.rules.TestRule;
-import org.junit.runner.Description;
-import org.junit.runners.model.Statement;
-import org.slf4j.LoggerFactory;
-
-/**
- * JUnit {@link TestRule} similar to the TestRule in {@code log4j-core} of the same name.
- *
- * @since 2.1
- * @since 2.4.1 Renamed InitialLoggerContext to LoggerContextRule
- */
-public class LoggerContextRule implements TestRule {
-
-    private final String configLocation;
-
-    private LoggerContext context;
-
-    private String testClassName;
-
-    public LoggerContextRule(final String configLocation) {
-        this.configLocation = configLocation;
-    }
-
-    @Override
-    public Statement apply(final Statement base, final Description description) {
-        testClassName = description.getClassName();
-        return new Statement() {
-            @Override
-            public void evaluate() throws Throwable {
-                context = (LoggerContext) LoggerFactory.getILoggerFactory();
-                final GenericConfigurator configurator = new JoranConfigurator();
-                configurator.setContext(context);
-                configurator.doConfigure(configLocation);
-                base.evaluate();
-            }
-        };
-    }
-
-    public LoggerContext getContext() {
-        return context;
-    }
-
-    public Logger getLogger() {
-        return context.getLogger(testClassName);
-    }
-
-    public Logger getLogger(final String name) {
-        return context.getLogger(name);
-    }
-}
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextSource.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextSource.java
new file mode 100644
index 0000000000..d71921c21f
--- /dev/null
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerContextSource.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.slf4j;
+
+import java.lang.annotation.Documented;
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Inherited;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+import org.apache.logging.log4j.test.junit.TempLoggingDirectory;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.extension.ExtendWith;
+
+/**
+ * Specifies a configuration file to use for unit tests.
+ *
+ * This is similar to the org.apache.logging.log4j.core.junit.LoggerContextSource annotation for Log4j.
+ */
+@Retention(RetentionPolicy.RUNTIME)
+@Target({ElementType.TYPE, ElementType.METHOD})
+@Documented
+@Inherited
+@Tag("functional")
+@ExtendWith(TempLoggingDirectory.class)
+@ExtendWith(LoggerContextResolver.class)
+@ExtendWith(LoggerResolver.class)
+public @interface LoggerContextSource {
+    /**
+     * Specifies the name of the configuration file to use for the annotated test.
+     * <p>
+     * Defaults to the fully qualified name of the test class with '.xml' appended.
+     * E.g. this class would have a default of
+     * {@code org/apache/logging/log4j/core/test/junit/LoggerContextSource.xml}.
+     * </p>
+     */
+    String value() default "";
+}
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerResolver.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerResolver.java
new file mode 100644
index 0000000000..28f94113a3
--- /dev/null
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerResolver.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you 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
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.slf4j;
+
+import ch.qos.logback.classic.Logger;
+import org.apache.logging.log4j.test.junit.ExtensionContextAnchor;
+import org.apache.logging.slf4j.LoggerContextResolver.LoggerContextHolder;
+import org.junit.jupiter.api.extension.BeforeAllCallback;
+import org.junit.jupiter.api.extension.BeforeEachCallback;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.api.extension.ExtensionContextException;
+import org.junit.jupiter.api.extension.ParameterContext;
+import org.junit.jupiter.api.extension.ParameterResolutionException;
+import org.junit.jupiter.api.extension.support.TypeBasedParameterResolver;
+import org.junit.platform.commons.support.HierarchyTraversalMode;
+import org.junit.platform.commons.support.ModifierSupport;
+import org.junit.platform.commons.support.ReflectionSupport;
+
+public class LoggerResolver extends TypeBasedParameterResolver<Logger>
+        implements BeforeAllCallback, BeforeEachCallback {
+
+    private static final Object KEY = LoggerContextHolder.class;
+
+    @Override
+    public Logger resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
+            throws ParameterResolutionException {
+        return ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext)
+                .getLogger();
+    }
+
+    @Override
+    public void beforeAll(ExtensionContext extensionContext) throws Exception {
+        final LoggerContextHolder holder =
+                ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext);
+        if (holder != null) {
+            ReflectionSupport.findFields(
+                            extensionContext.getRequiredTestClass(),
+                            f -> ModifierSupport.isStatic(f) && f.getType().equals(Logger.class),
+                            HierarchyTraversalMode.TOP_DOWN)
+                    .forEach(f -> {
+                        try {
+                            f.setAccessible(true);
+                            f.set(null, holder.getLogger());
+                        } catch (ReflectiveOperationException e) {
+                            throw new ExtensionContextException("Failed to inject field " + f, e);
+                        }
+                    });
+        }
+    }
+
+    @Override
+    public void beforeEach(ExtensionContext extensionContext) throws Exception {
+        final LoggerContextHolder holder =
+                ExtensionContextAnchor.getAttribute(KEY, LoggerContextHolder.class, extensionContext);
+        if (holder != null) {
+            ReflectionSupport.findFields(
+                            extensionContext.getRequiredTestClass(),
+                            f -> ModifierSupport.isNotStatic(f) && f.getType().equals(Logger.class),
+                            HierarchyTraversalMode.TOP_DOWN)
+                    .forEach(f -> {
+                        try {
+                            f.setAccessible(true);
+                            f.set(extensionContext.getRequiredTestInstance(), holder.getLogger());
+                        } catch (ReflectiveOperationException e) {
+                            throw new ExtensionContextException("Failed to inject field " + f, e);
+                        }
+                    });
+        }
+    }
+}
diff --git a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java
index 41e5e75e73..cda5cc8d63 100644
--- a/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java
+++ b/log4j-to-slf4j/src/test/java/org/apache/logging/slf4j/LoggerTest.java
@@ -20,6 +20,7 @@ import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.*;
 import static org.junit.Assert.*;
 
+import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.spi.ILoggingEvent;
 import ch.qos.logback.core.testUtil.StringListAppender;
 import java.lang.reflect.InvocationTargetException;
@@ -33,30 +34,28 @@ import org.apache.logging.log4j.message.MessageFactory;
 import org.apache.logging.log4j.message.ParameterizedMessageFactory;
 import org.apache.logging.log4j.message.StringFormatterMessageFactory;
 import org.apache.logging.log4j.spi.LoggingSystem;
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
 import org.slf4j.MDC;
 
-/**
- *
- */
+@UsingStatusListener
+@LoggerContextSource
 public class LoggerTest {
 
-    private static final String CONFIG = "target/test-classes/logback-slf4j.xml";
-
-    @ClassRule
-    public static final LoggerContextRule CTX = new LoggerContextRule(CONFIG);
-
+    private static final String STRING = "String";
+    // Log4j objects
     private Logger logger;
+    // Logback objects
+    private LoggerContext context;
     private StringListAppender<ILoggingEvent> list;
 
-    @Before
+    @BeforeEach
     public void setUp() throws Exception {
-        final org.slf4j.Logger slf4jLogger = CTX.getLogger();
+        final org.slf4j.Logger slf4jLogger = context.getLogger(getClass());
         logger = LogManager.getLogger();
         assertThat(slf4jLogger, is(theInstance(((SLF4JLogger) logger).getLogger())));
-        final ch.qos.logback.classic.Logger rootLogger = CTX.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
+        final ch.qos.logback.classic.Logger rootLogger = context.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
         rootLogger.detachAppender("console");
         list = TestUtil.getListAppender(rootLogger, "LIST");
         assertThat(list, is(notNullValue()));
@@ -73,7 +72,7 @@ public class LoggerTest {
 
     @Test
     public void simpleFlow() {
-        logger.traceEntry(CONFIG);
+        logger.traceEntry(STRING);
         logger.traceExit(0);
         assertThat(list.strList, hasSize(2));
     }
@@ -163,7 +162,7 @@ public class LoggerTest {
 
     @Test
     public void paramIncludesSubstitutionMarker_nonLocationAware() {
-        final org.slf4j.Logger slf4jLogger = CTX.getLogger();
+        final org.slf4j.Logger slf4jLogger = context.getLogger(getClass());
         final Logger nonLocationAwareLogger =
                 new SLF4JLogger(slf4jLogger.getName(), (org.slf4j.Logger) Proxy.newProxyInstance(
                         getClass().getClassLoader(), new Class<?>[] {org.slf4j.Logger.class}, (proxy, method, args) -> {
diff --git a/log4j-to-slf4j/src/test/resources/logback-calling-class.xml b/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/CallerInformationTest.xml
similarity index 100%
rename from log4j-to-slf4j/src/test/resources/logback-calling-class.xml
rename to log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/CallerInformationTest.xml
diff --git a/log4j-to-slf4j/src/test/resources/logback-turbofilter.xml b/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LogBuilderTest.xml
similarity index 100%
rename from log4j-to-slf4j/src/test/resources/logback-turbofilter.xml
rename to log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LogBuilderTest.xml
diff --git a/log4j-to-slf4j/src/test/resources/logback-slf4j.xml b/log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LoggerTest.xml
similarity index 100%
rename from log4j-to-slf4j/src/test/resources/logback-slf4j.xml
rename to log4j-to-slf4j/src/test/resources/org/apache/logging/slf4j/LoggerTest.xml