This is an automated email from the ASF dual-hosted git repository.
garydgregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-logging.git
The following commit(s) were added to refs/heads/master by this push:
new 4271893 Reject SLF4J-to-JCL logging delegation loops (#436)
4271893 is described below
commit 42718937d2909a22f318e859faf8b08b11271460
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Nov 27 10:49:53 2026 -0500
Reject SLF4J-to-JCL logging delegation loops (#436)
Detect the active slf4j-jcl binding before creating SLF4J loggers.
Throw an actionable LogConfigurationException instead of hanging on
Java 8 or failing with a recursive update on newer Java versions.
Add isolated regression coverage for both discovery paths, explicit
legacy factory configuration, and ignored bindings under SLF4J 2.
Document factory selection and configuration workarounds.
---
pom.xml | 192 +++++++++++++++++++++
src/changes/changes.xml | 1 +
.../commons/logging/impl/Slf4jLogFactory.java | 15 +-
.../org/apache/commons/logging/package-info.java | 48 +++---
src/site/xdoc/guide.xml | 34 +++-
src/site/xdoc/troubleshooting.xml | 31 +++-
.../logging/slf4j/jcl/DelegationLoopTest.java | 69 ++++++++
.../logging/slf4j/jcl/IgnoredBindingTest.java | 47 +++++
.../logging/slf4j/jcl/LegacyFactoryTest.java | 44 +++++
9 files changed, 453 insertions(+), 28 deletions(-)
diff --git a/pom.xml b/pom.xml
index 54cc426..b95a298 100644
--- a/pom.xml
+++ b/pom.xml
@@ -59,6 +59,8 @@ under the License.
<log4j2.version>2.26.1</log4j2.version>
<logback.version>1.3.16</logback.version>
<slf4j.version>2.0.20</slf4j.version>
+ <!-- Matches the legacy binding involved in the delegation loop
regression. -->
+ <slf4j1.version>1.7.25</slf4j1.version>
<findsecbugs.version>1.14.0</findsecbugs.version>
<jakarta.servlet-api.version>5.0.0</jakarta.servlet-api.version>
<commons.osgi.import>
@@ -429,6 +431,196 @@ under the License.
</includes>
</configuration>
</execution>
+ <!-- Reject the active SLF4J 1 binding that delegates back to JCL.
-->
+ <execution>
+ <id>slf4j-jcl-test</id>
+ <goals>
+ <goal>integration-test</goal>
+ </goals>
+ <configuration>
+ <!-- Use the packaged JAR for case-sensitive class lookup,
including on macOS. -->
+
<classesDirectory>${project.build.directory}/${project.build.finalName}.jar</classesDirectory>
+ <forkedProcessTimeoutInSeconds>60</forkedProcessTimeoutInSeconds>
+ <reportNameSuffix>slf4j-jcl-test</reportNameSuffix>
+ <additionalClasspathDependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j1.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jcl</artifactId>
+ <version>${slf4j1.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </additionalClasspathDependencies>
+ <classpathDependencyExcludes>
+ <exclude>org.apache.logging.log4j:*</exclude>
+ <exclude>org.slf4j:*</exclude>
+ </classpathDependencyExcludes>
+ <includes>
+
<include>org/apache/commons/logging/slf4j/jcl/DelegationLoopTest.java</include>
+ </includes>
+ </configuration>
+ </execution>
+ <!-- Reject the same loop when Log4j API redirects to SLF4J. -->
+ <execution>
+ <id>log4j-to-slf4j-jcl-test</id>
+ <goals>
+ <goal>integration-test</goal>
+ </goals>
+ <configuration>
+ <!-- Use the packaged JAR for case-sensitive class lookup,
including on macOS. -->
+
<classesDirectory>${project.build.directory}/${project.build.finalName}.jar</classesDirectory>
+ <forkedProcessTimeoutInSeconds>60</forkedProcessTimeoutInSeconds>
+ <reportNameSuffix>log4j-to-slf4j-jcl-test</reportNameSuffix>
+ <additionalClasspathDependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j1.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jcl</artifactId>
+ <version>${slf4j1.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.logging.log4j</groupId>
+ <artifactId>log4j-to-slf4j</artifactId>
+ <version>${log4j2.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </additionalClasspathDependencies>
+ <classpathDependencyExcludes>
+ <exclude>org.apache.logging.log4j:log4j-core</exclude>
+ <exclude>org.apache.logging.log4j:log4j-core-test</exclude>
+ <exclude>org.apache.logging.log4j:log4j-1.2-api</exclude>
+ <exclude>org.slf4j:*</exclude>
+ </classpathDependencyExcludes>
+ <includes>
+
<include>org/apache/commons/logging/slf4j/jcl/DelegationLoopTest.java</include>
+ </includes>
+ </configuration>
+ </execution>
+ <!-- An explicit legacy JCL factory breaks the delegation loop. -->
+ <execution>
+ <id>slf4j-jcl-legacy-test</id>
+ <goals>
+ <goal>integration-test</goal>
+ </goals>
+ <configuration>
+ <!-- Use the packaged JAR for case-sensitive class lookup,
including on macOS. -->
+
<classesDirectory>${project.build.directory}/${project.build.finalName}.jar</classesDirectory>
+ <forkedProcessTimeoutInSeconds>60</forkedProcessTimeoutInSeconds>
+ <reportNameSuffix>slf4j-jcl-legacy-test</reportNameSuffix>
+ <additionalClasspathDependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ <version>${slf4j1.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jcl</artifactId>
+ <version>${slf4j1.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </additionalClasspathDependencies>
+ <classpathDependencyExcludes>
+ <exclude>org.apache.logging.log4j:*</exclude>
+ <exclude>org.slf4j:*</exclude>
+ </classpathDependencyExcludes>
+ <includes>
+
<include>org/apache/commons/logging/slf4j/jcl/LegacyFactoryTest.java</include>
+ </includes>
+ <systemPropertyVariables>
+
<org.apache.commons.logging.LogFactory>org.apache.commons.logging.impl.LogFactoryImpl</org.apache.commons.logging.LogFactory>
+
<org.apache.commons.logging.Log>org.apache.commons.logging.impl.NoOpLog</org.apache.commons.logging.Log>
+ </systemPropertyVariables>
+ </configuration>
+ </execution>
+ <!-- A legacy binding ignored by SLF4J 2 must not prevent logging.
-->
+ <execution>
+ <id>slf4j2-jcl-test</id>
+ <goals>
+ <goal>integration-test</goal>
+ </goals>
+ <configuration>
+ <!-- Use the packaged JAR for case-sensitive class lookup,
including on macOS. -->
+
<classesDirectory>${project.build.directory}/${project.build.finalName}.jar</classesDirectory>
+ <forkedProcessTimeoutInSeconds>60</forkedProcessTimeoutInSeconds>
+ <reportNameSuffix>slf4j2-jcl-test</reportNameSuffix>
+ <additionalClasspathDependencies>
+ <dependency>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-jcl</artifactId>
+ <version>${slf4j1.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ </exclusion>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ <dependency>
+ <groupId>ch.qos.logback</groupId>
+ <artifactId>logback-classic</artifactId>
+ <version>${logback.version}</version>
+ <exclusions>
+ <exclusion>
+ <groupId>org.slf4j</groupId>
+ <artifactId>slf4j-api</artifactId>
+ </exclusion>
+ </exclusions>
+ </dependency>
+ </additionalClasspathDependencies>
+ <classpathDependencyExcludes>
+ <exclude>org.apache.logging.log4j:*</exclude>
+ </classpathDependencyExcludes>
+ <includes>
+
<include>org/apache/commons/logging/slf4j/jcl/IgnoredBindingTest.java</include>
+ </includes>
+ </configuration>
+ </execution>
</executions>
</plugin>
<plugin>
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 0420a50..f83333c 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -47,6 +47,7 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="1.4.1" date="YYYY-MM-DD" description="This is a feature
and maintenance release. Java 8 or later is required.">
<!-- FIX -->
+ <action type="fix">Reject an active SLF4J-to-JCL binding with an
actionable exception instead of hanging or failing with a recursive
update.</action>
<!-- ADD -->
<!-- UPDATE -->
<action dev="ggregory" type="update" due-to="Gary Gregory">Bump
org.apache.commons:commons-parent from 102 to 107.</action>
diff --git a/src/main/java/org/apache/commons/logging/impl/Slf4jLogFactory.java
b/src/main/java/org/apache/commons/logging/impl/Slf4jLogFactory.java
index 0fae839..92e22f1 100644
--- a/src/main/java/org/apache/commons/logging/impl/Slf4jLogFactory.java
+++ b/src/main/java/org/apache/commons/logging/impl/Slf4jLogFactory.java
@@ -288,10 +288,23 @@ public final class Slf4jLogFactory extends LogFactory {
return getInstance(clazz.getName());
}
+ /**
+ * Gets a logger, rejecting an SLF4J binding that delegates back to
Commons Logging.
+ *
+ * @param name the logger name.
+ * @return the logger.
+ * @throws LogConfigurationException if the active SLF4J binding delegates
to Commons Logging.
+ */
@Override
public Log getInstance(final String name) {
+ final ILoggerFactory factory = LoggerFactory.getILoggerFactory();
+ // Check the active factory, since SLF4J 2 can ignore a legacy binding
on the class path.
+ if
("org.slf4j.impl.JCLLoggerFactory".equals(factory.getClass().getName())) {
+ throw new LogConfigurationException("SLF4J is bound to JCL
(slf4j-jcl), which would cause a logging delegation loop. " +
+ "Remove slf4j-jcl or set " + FACTORY_PROPERTY + " to " +
FACTORY_DEFAULT + ".");
+ }
return loggers.computeIfAbsent(name, n -> {
- final Logger logger = LoggerFactory.getLogger(n);
+ final Logger logger = factory.getLogger(n);
return logger instanceof LocationAwareLogger ? new
Slf4jLocationAwareLog((LocationAwareLogger) logger) : new Slf4jLog(
logger);
});
diff --git a/src/main/java/org/apache/commons/logging/package-info.java
b/src/main/java/org/apache/commons/logging/package-info.java
index f5ab9fe..ca44499 100644
--- a/src/main/java/org/apache/commons/logging/package-info.java
+++ b/src/main/java/org/apache/commons/logging/package-info.java
@@ -61,10 +61,9 @@
* ...
* }
* </pre>
- * <p>Unless you configure things differently, all log output will be written
- * to System.err. Therefore, you really will want to review the remainder of
- * this page in order to understand how to configure logging for your
- * application.</p>
+ * <p>
+ * Log output is handled by the selected logging backend. Review the remainder
of this page to configure logging for your application.
+ * </p>
* <h2>Configuring the Commons Logging Package</h2>
* <h3>Choosing a {@code LogFactory} Implementation</h3>
* <p>From an application perspective, the first requirement is to retrieve an
@@ -87,8 +86,9 @@
* visible in the application class path, with a property named
* {@code org.apache.commons.logging.LogFactory} defining the
* desired implementation class name.</li>
- * <li>Fall back to a default implementation, which is described
- * further below.</li>
+ * <li>Automatically select {@link
org.apache.commons.logging.impl.Log4jApiLogFactory} when Log4j API is available
and is not redirected to SLF4J,
+ * otherwise {@link org.apache.commons.logging.impl.Slf4jLogFactory} when
SLF4J is available,
+ * otherwise the legacy {@link
org.apache.commons.logging.impl.LogFactoryImpl}.</li>
* </ul>
* <p>If a {@code commons-logging.properties} file is found, all of the
* properties defined there are also used to set configuration attributes on
@@ -101,21 +101,31 @@
* but still allow each web application to provide its own {@code LogFactory}
* implementation, if it so desires. An instance of this class will then be
* created, and cached per class loader.
- * <h3>The Default {@code LogFactory} Implementation</h3>
- * <p>The Logging Package APIs include a default {@code LogFactory}
- * implementation class (<a href="impl/LogFactoryImpl.html">
- * org.apache.commons.logging.impl.LogFactoryImpl</a>) that is selected if no
- * other implementation class name can be discovered. Its primary purpose is
- * to create (as necessary) and return <a href="Log.html">Log</a> instances
- * in response to calls to the {@code getInstance()} method. The default
- * implementation uses the following rules:</p>
+ * <h3>The Legacy {@code LogFactory} Implementation</h3>
+ * <p>
+ * The legacy {@link org.apache.commons.logging.impl.LogFactoryImpl} creates
and returns {@link org.apache.commons.logging.Log} instances.
+ * The {@code org.apache.commons.logging.Log} property selects a logger only
for this factory; it does not override factory discovery.
+ * To select {@code NoOpLog} even when Log4j API or SLF4J is available, use
both settings in {@code commons-logging.properties}:
+ * </p>
+ * <pre>
+ *
org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
+ * org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog
+ * </pre>
+ * <p>
+ * Replace {@code NoOpLog} with {@code SimpleLog} to use the simple logger.
These settings can also be supplied as system properties.
+ * Do not combine {@code Slf4jLogFactory} with an active {@code slf4j-jcl}
binding, which delegates back to JCL and creates a logging loop.
+ * Remove that binding or explicitly select the legacy factory to retain the
SLF4J-to-JCL direction.
+ * </p>
+ * <p>
+ * The legacy implementation uses the following rules:
+ * </p>
* <ul>
* <li>At most one {@code Log} instance of the same name will be created.
* Subsequent {@code getInstance()} calls to the same
* {@code LogFactory} instance, with the same name or {@code Class}
* parameter, will return the same {@code Log} instance.</li>
- * <li>When a new {@code Log} instance must be created, the default
- * {@code LogFactory} implementation uses the following discovery
+ * <li>When a new {@code Log} instance must be created, the legacy
+ * {@code LogFactoryImpl} implementation uses the following discovery
* process:
* <ul>
* <li>Look for a configuration attribute of this factory named
@@ -126,11 +136,7 @@
* {@code org.apache.commons.logging.Log} (for backwards
* compatibility to pre-1.0 versions of this API, a system property
* {@code org.apache.commons.logging.log} is also consulted).</li>
- * <li>If the Log4J logging system is available in the application
- * class path, use the corresponding wrapper class
- * (<a href="impl/Log4JLogger.html">Log4JLogger</a>).</li>
- * <li>If the application is executing on a JDK 1.4 system, use
- * the corresponding wrapper class
+ * <li>If the {@code java.logging} module is available, use the corresponding
wrapper class
* (<a href="impl/Jdk14Logger.html">Jdk14Logger</a>).</li>
* <li>Fall back to the default simple logging implementation
* (<a href="impl/SimpleLog.html">SimpleLog</a>).</li>
diff --git a/src/site/xdoc/guide.xml b/src/site/xdoc/guide.xml
index eda8f3c..82da31a 100644
--- a/src/site/xdoc/guide.xml
+++ b/src/site/xdoc/guide.xml
@@ -136,9 +136,9 @@ JCL's discovery process).
There are two base abstractions used by JCL: <code>Log</code>
(the basic logger) and <code>LogFactory</code> (which knows how to create
<code>Log</code>
instances). Specifying a particular Log implementation is very useful (whether
that is
-one provided by Commons Logging or a user-defined one). Specifying a
-<code>LogFactory</code> implementation explicitly is a subject for
-advanced users only, so will not be addressed here.
+one provided by Commons Logging or a user-defined one). Factory selection
happens before
+logger selection. The <code>org.apache.commons.logging.Log</code> property
selects a logger
+only when the legacy <code>LogFactoryImpl</code> is used.
</p>
<p>
JCL provides three standard log factories:
@@ -152,6 +152,25 @@ advanced users only, so will not be addressed here.
<li>otherwise the legacy <code>LogFactoryImpl</code> is
used.</li>
</ol>
<p>
+Since version 1.3.0, the presence of Log4j API or SLF4J can therefore change
the selected factory.
+To select <code>NoOpLog</code> explicitly, for example to disable logging in
tests, put both
+settings in <code>commons-logging.properties</code>:
+ </p>
+
<source>org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
+org.apache.commons.logging.Log=org.apache.commons.logging.impl.NoOpLog</source>
+ <p>
+For <code>SimpleLog</code>, replace <code>NoOpLog</code> with
<code>SimpleLog</code> in the second setting.
+The same settings can be supplied as system properties. A factory selected by
a system property or
+service provider takes precedence over the factory setting in
<code>commons-logging.properties</code>.
+ </p>
+ <p>
+Do not combine <code>Slf4jLogFactory</code> with an active
<code>slf4j-jcl</code> binding:
+Commons Logging would delegate to SLF4J, which would delegate back to Commons
Logging.
+Remove the reverse binding and configure an SLF4J backend, or explicitly select
+<code>LogFactoryImpl</code> to retain the SLF4J-to-JCL direction.
+See <a href="troubleshooting.html#SLF4J_Delegation_Loops">SLF4J Delegation
Loops</a>.
+ </p>
+ <p>
The legacy <code>LogFactory</code> implementation uses the following discovery
process
to determine what type of <code>Log</code> implementation it should use
(the process terminates when the first positive match - in order - is found):
@@ -693,8 +712,8 @@ can be provided to meet
specific requirements for connecting to, or instantiating, a logger.
</p>
<p>
-The default <code>LogFactory</code> provided by JCL
-can be configured to instantiate a specific implementation of the
+The legacy <code>LogFactoryImpl</code>, selected explicitly as described in
+<a href="#Configuration">Configuration</a>, can be configured to instantiate a
specific implementation of the
<code>org.apache.commons.logging.Log</code> interface
by setting the property of the same name
(<code>org.apache.commons.logging.Log</code>).
This property can be specified as a system property,
@@ -722,6 +741,11 @@ implementation. Developers requiring a fully functional
open source logging syst
directed to <a href='https://logging.apache.org/log4j'>Log4J</a>.
</p>
<p>
+To select <code>SimpleLog</code> even when Log4j API or SLF4J is available,
use:
+ </p>
+
<source>org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl
+org.apache.commons.logging.Log=org.apache.commons.logging.impl.SimpleLog</source>
+ <p>
<code>SimpleLog</code> sends all (enabled) log messages,
for all defined loggers, to <code>System.err</code>. The following system
properties
are supported to configure the behavior of this logger:</p>
diff --git a/src/site/xdoc/troubleshooting.xml
b/src/site/xdoc/troubleshooting.xml
index 1013b60..f5727d7 100644
--- a/src/site/xdoc/troubleshooting.xml
+++ b/src/site/xdoc/troubleshooting.xml
@@ -34,6 +34,9 @@
<a href="#Contents">Contents</a>
</li>
<li>
+<a href="#SLF4J Delegation Loops">SLF4J Delegation Loops</a>
+</li>
+<li>
<a href="#Using JCL Diagnostics">Using JCL Diagnostics</a>
<ul>
<li>
@@ -97,6 +100,31 @@
</li>
</ul>
</section>
+ <section name='SLF4J Delegation Loops'>
+ <p>
+Since version 1.3.0, Commons Logging can automatically select
<code>Slf4jLogFactory</code>.
+If SLF4J is bound to Commons Logging through <code>slf4j-jcl</code>,
requesting a logger creates
+a delegation loop: JCL to SLF4J to JCL. Before version 1.4.1, this
configuration could hang
+in <code>ConcurrentHashMap.computeIfAbsent</code> on Java 8 or throw
+<code>IllegalStateException: Recursive update</code> on newer Java versions.
+ </p>
+ <p>
+Starting with version 1.4.1, <code>Slf4jLogFactory</code> rejects the active
JCL binding with a
+<code>LogConfigurationException</code> explaining the delegation loop. An old
binding JAR ignored
+by SLF4J 2 does not trigger this check.
+ </p>
+ <p>
+Remove <code>slf4j-jcl</code> and choose an SLF4J backend to route logging
through SLF4J.
+If the application must instead route SLF4J to JCL, explicitly select the
legacy JCL factory
+in <code>commons-logging.properties</code> or with a system property:
+ </p>
+
<source>org.apache.commons.logging.LogFactory=org.apache.commons.logging.impl.LogFactoryImpl</source>
+ <p>
+The legacy factory can then use <code>java.util.logging</code>,
<code>SimpleLog</code>, or
+<code>NoOpLog</code>. Setting only <code>org.apache.commons.logging.Log</code>
does not override
+factory discovery. See the <a href="guide.html#Configuration">configuration
examples</a>.
+ </p>
+ </section>
<section name='Using JCL Diagnostics'>
<p>
Diagnostics is a feature introduced in JCL 1.1 as an aid to debugging problems
@@ -231,7 +259,8 @@ Finally, once initialization is complete a <code>BOOTSTRAP
COMPLETED</code> mess
</subsection>
<subsection name='Construction Of LogFactoryImpl Instances'>
<p>
-<code>LogFactoryImpl</code> is the standard and default
<code>LogFactory</code> implementation.
+<code>LogFactoryImpl</code> is the legacy <code>LogFactory</code>
implementation, used when explicitly selected
+or when the Log4j API and SLF4J factories are unavailable.
This section obviously only applies to configurations using this
implementation.
</p>
<p>
diff --git
a/src/test/java/org/apache/commons/logging/slf4j/jcl/DelegationLoopTest.java
b/src/test/java/org/apache/commons/logging/slf4j/jcl/DelegationLoopTest.java
new file mode 100644
index 0000000..af8fd80
--- /dev/null
+++ b/src/test/java/org/apache/commons/logging/slf4j/jcl/DelegationLoopTest.java
@@ -0,0 +1,69 @@
+/*
+ * 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
+ *
+ * https://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.commons.logging.slf4j.jcl;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import org.apache.commons.logging.LogConfigurationException;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.impl.Slf4jLogFactory;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.function.Executable;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests rejection of an active SLF4J binding that delegates back to Commons
Logging.
+ */
+class DelegationLoopTest {
+
+ /**
+ * Checks that a logger request explains the delegation loop and its
remedies.
+ *
+ * @param request the logger request.
+ */
+ private static void assertDelegationLoop(final Executable request) {
+ final LogConfigurationException exception =
assertThrows(LogConfigurationException.class, request);
+ assertTrue(exception.getMessage().contains("slf4j-jcl"));
+ assertTrue(exception.getMessage().contains("delegation loop"));
+
assertTrue(exception.getMessage().contains(LogFactory.FACTORY_PROPERTY));
+
assertTrue(exception.getMessage().contains(LogFactory.FACTORY_DEFAULT));
+ }
+
+ /**
+ * Tests automatic selection with named and class loggers, including
repeated requests.
+ */
+ @Test
+ void testAutomaticFactory() {
+ assertEquals("org.slf4j.impl.JCLLoggerFactory",
LoggerFactory.getILoggerFactory().getClass().getName());
+ assertEquals(Slf4jLogFactory.class,
LogFactory.getFactory().getClass());
+ assertDelegationLoop(() -> LogFactory.getLog("delegation-loop"));
+ assertDelegationLoop(() -> LogFactory.getLog("delegation-loop"));
+ assertDelegationLoop(() -> LogFactory.getLog(getClass()));
+ }
+
+ /**
+ * Tests explicit factory construction, which bypasses automatic discovery.
+ */
+ @Test
+ void testExplicitFactory() {
+ final Slf4jLogFactory factory = new Slf4jLogFactory();
+ assertDelegationLoop(() -> factory.getInstance("explicit-factory"));
+ }
+}
diff --git
a/src/test/java/org/apache/commons/logging/slf4j/jcl/IgnoredBindingTest.java
b/src/test/java/org/apache/commons/logging/slf4j/jcl/IgnoredBindingTest.java
new file mode 100644
index 0000000..465ce92
--- /dev/null
+++ b/src/test/java/org/apache/commons/logging/slf4j/jcl/IgnoredBindingTest.java
@@ -0,0 +1,47 @@
+/*
+ * 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
+ *
+ * https://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.commons.logging.slf4j.jcl;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.impl.Slf4jLogFactory;
+import org.junit.jupiter.api.Test;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests that a legacy JCL binding ignored by SLF4J 2 does not prevent logging.
+ */
+class IgnoredBindingTest {
+
+ /**
+ * Tests logger creation and reuse with a valid provider and a dormant
reverse binding.
+ */
+ @Test
+ void testIgnoredBinding() {
+
assertNotNull(getClass().getClassLoader().getResource("org/slf4j/impl/JCLLoggerFactory.class"));
+ assertEquals("ch.qos.logback.classic.LoggerContext",
LoggerFactory.getILoggerFactory().getClass().getName());
+ assertEquals(Slf4jLogFactory.class,
LogFactory.getFactory().getClass());
+ final Log log = LogFactory.getLog(getClass());
+ assertSame(log, LogFactory.getLog(getClass().getName()));
+ log.info("A dormant SLF4J binding does not cause a delegation loop.");
+ }
+}
diff --git
a/src/test/java/org/apache/commons/logging/slf4j/jcl/LegacyFactoryTest.java
b/src/test/java/org/apache/commons/logging/slf4j/jcl/LegacyFactoryTest.java
new file mode 100644
index 0000000..e88a8ea
--- /dev/null
+++ b/src/test/java/org/apache/commons/logging/slf4j/jcl/LegacyFactoryTest.java
@@ -0,0 +1,44 @@
+/*
+ * 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
+ *
+ * https://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.commons.logging.slf4j.jcl;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+
+import org.apache.commons.logging.LogFactory;
+import org.apache.commons.logging.impl.LogFactoryImpl;
+import org.apache.commons.logging.impl.NoOpLog;
+import org.junit.jupiter.api.Test;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Tests explicit legacy factory configuration with an SLF4J binding to
Commons Logging.
+ */
+class LegacyFactoryTest {
+
+ /**
+ * Tests that both logging APIs reach the configured NoOpLog without a
delegation loop.
+ */
+ @Test
+ void testExplicitLegacyFactory() {
+ assertEquals("org.slf4j.impl.JCLLoggerFactory",
LoggerFactory.getILoggerFactory().getClass().getName());
+ assertEquals(LogFactoryImpl.class, LogFactory.getFactory().getClass());
+ assertEquals(NoOpLog.class, LogFactory.getLog(getClass()).getClass());
+ assertFalse(LoggerFactory.getLogger(getClass()).isErrorEnabled());
+ }
+}