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/10/20 14:22:21 UTC

[logging-log4j2] 02/02: Fix JCL to Log4j API bridge

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

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

commit 232f3b0f3bb6370189ef2b75b8de8cd65ed473e5
Author: Piotr P. Karwasz <pi...@karwasz.org>
AuthorDate: Fri Oct 20 16:22:11 2023 +0200

    Fix JCL to Log4j API bridge
---
 log4j-jcl/pom.xml                                  |  8 ++---
 .../services/org.apache.commons.logging.LogFactory | 12 +++++++
 .../logging/log4j/jcl/CallerInformationTest.java   | 39 ++++++++++----------
 .../org/apache/logging/log4j/jcl/LoggerTest.java   | 38 ++++++++++----------
 .../logging/log4j/jcl/CallerInformationTest.xml    | 42 ++++++++++++++++++++++
 .../apache/logging/log4j/jcl/LoggerTest.xml}       |  0
 .../.2.x.x/1865_fix_apache_commons_logging.xml     | 27 ++++++++++++++
 src/site/_release-notes.adoc                       |  1 +
 src/site/_release-notes/_2.x.x.adoc                |  4 +++
 9 files changed, 128 insertions(+), 43 deletions(-)

diff --git a/log4j-jcl/pom.xml b/log4j-jcl/pom.xml
index 345e2ad4e2..e178c6aed7 100644
--- a/log4j-jcl/pom.xml
+++ b/log4j-jcl/pom.xml
@@ -60,13 +60,13 @@
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>org.junit.jupiter</groupId>
-      <artifactId>junit-jupiter-engine</artifactId>
+      <groupId>org.assertj</groupId>
+      <artifactId>assertj-core</artifactId>
       <scope>test</scope>
     </dependency>
     <dependency>
-      <groupId>org.junit.vintage</groupId>
-      <artifactId>junit-vintage-engine</artifactId>
+      <groupId>org.junit.jupiter</groupId>
+      <artifactId>junit-jupiter-api</artifactId>
       <scope>test</scope>
     </dependency>
   </dependencies>
diff --git a/log4j-jcl/src/main/resources/META-INF/services/org.apache.commons.logging.LogFactory b/log4j-jcl/src/main/resources/META-INF/services/org.apache.commons.logging.LogFactory
new file mode 100644
index 0000000000..f18955c07b
--- /dev/null
+++ b/log4j-jcl/src/main/resources/META-INF/services/org.apache.commons.logging.LogFactory
@@ -0,0 +1,12 @@
+org.apache.logging.log4j.jcl.LogFactoryImpl
+###
+#  ██     ██  █████  ██████  ███    ██ ██ ███    ██  ██████  ██
+#  ██     ██ ██   ██ ██   ██ ████   ██ ██ ████   ██ ██       ██
+#  ██  █  ██ ███████ ██████  ██ ██  ██ ██ ██ ██  ██ ██   ███ ██
+#  ██ ███ ██ ██   ██ ██   ██ ██  ██ ██ ██ ██  ██ ██ ██    ██
+#   ███ ███  ██   ██ ██   ██ ██   ████ ██ ██   ████  ██████  ██
+#
+# Apache Commons Logging 1.2 does **not** use the `ServiceLoader` class.
+# It reads the first line of this file and expects it to be a FQCN.
+#
+# cf. https://github.com/apache/logging-log4j2/issues/1865
diff --git a/log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/CallerInformationTest.java b/log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/CallerInformationTest.java
index 22cb7a75b6..e1f2d7b59d 100644
--- a/log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/CallerInformationTest.java
+++ b/log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/CallerInformationTest.java
@@ -20,38 +20,38 @@ import java.util.List;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.test.appender.ListAppender;
-import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
-import org.junit.ClassRule;
-import org.junit.Test;
+import org.apache.logging.log4j.test.junit.SetTestProperty;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
 
-import static org.junit.Assert.assertEquals;
+import static org.assertj.core.api.Assertions.assertThat;
 
+@UsingStatusListener
+@SetTestProperty(key ="log4j2.configurationFile", value="org/apache/logging/log4j/jcl/CallerInformationTest.xml")
 public class CallerInformationTest {
 
-    // config from log4j-core test-jar
-    private static final String CONFIG = "log4j2-calling-class.xml";
-
-    @ClassRule
-    public static final LoggerContextRule ctx = new LoggerContextRule(CONFIG);
-
     @Test
     public void testClassLogger() throws Exception {
-        final ListAppender app = ctx.getListAppender("Class").clear();
+        final LoggerContext ctx = LoggerContext.getContext(false);
+        final ListAppender app = ctx.getConfiguration().getAppender("Class");
+        app.clear();
         final Log logger = LogFactory.getLog("ClassLogger");
         logger.info("Ignored message contents.");
         logger.warn("Verifying the caller class is still correct.");
         logger.error("Hopefully nobody breaks me!");
         final List<String> messages = app.getMessages();
-        assertEquals("Incorrect number of messages.", 3, messages.size());
-        for (final String message : messages) {
-            assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
-        }
+        assertThat(messages)
+                .hasSize(3)
+                .allMatch(c -> getClass().getName().equals(c));
     }
 
     @Test
     public void testMethodLogger() throws Exception {
-        final ListAppender app = ctx.getListAppender("Method").clear();
+        final LoggerContext ctx = LoggerContext.getContext(false);
+        final ListAppender app = ctx.getConfiguration().getAppender("Method");
+        app.clear();
         final Log logger = LogFactory.getLog("MethodLogger");
         logger.info("More messages.");
         logger.warn("CATASTROPHE INCOMING!");
@@ -59,9 +59,8 @@ public class CallerInformationTest {
         logger.warn("brains~~~");
         logger.info("Itchy. Tasty.");
         final List<String> messages = app.getMessages();
-        assertEquals("Incorrect number of messages.", 5, messages.size());
-        for (final String message : messages) {
-            assertEquals("Incorrect caller method name.", "testMethodLogger", message);
-        }
+        assertThat(messages)
+                .hasSize(5)
+                .allMatch("testMethodLogger"::equals);
     }
 }
diff --git a/log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/LoggerTest.java b/log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/LoggerTest.java
index a41d60e430..c26a16cd07 100644
--- a/log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/LoggerTest.java
+++ b/log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/LoggerTest.java
@@ -20,28 +20,27 @@ import java.util.List;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import org.apache.logging.log4j.core.LoggerContext;
 import org.apache.logging.log4j.core.test.appender.ListAppender;
-import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
+import org.apache.logging.log4j.test.junit.SetTestProperty;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
 import org.apache.logging.log4j.util.Strings;
-import org.junit.ClassRule;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
 
-import static org.hamcrest.Matchers.equalTo;
-import static org.hamcrest.Matchers.hasSize;
-import static org.junit.Assert.*;
+import static org.assertj.core.api.Assertions.assertThat;
 
-/**
- *
- */
-public class LoggerTest {
-
-    private static final String CONFIG = "log4j-test1.xml";
+@UsingStatusListener
+@SetTestProperty(key ="log4j2.configurationFile", value="org/apache/logging/log4j/jcl/LoggerTest.xml")
+class LoggerTest {
 
-    @ClassRule
-    public static final LoggerContextRule context = new LoggerContextRule(CONFIG);
+    @Test
+    void testFactory() {
+        final LogFactory factory = LogFactory.getFactory();
+        assertThat(factory).isInstanceOf(LogFactoryImpl.class);
+    }
 
     @Test
-    public void testLog() {
+    void testLog() {
         final Log logger = LogFactory.getLog("LoggerTest");
         logger.debug("Test message");
         verify("List", "o.a.l.l.j.LoggerTest Test message MDC{}" + Strings.LINE_SEPARATOR);
@@ -54,11 +53,12 @@ public class LoggerTest {
     }
 
     private void verify(final String name, final String expected) {
-        final ListAppender listApp = context.getListAppender(name);
+        final LoggerContext context = LoggerContext.getContext(false);
+        final ListAppender listApp = context.getConfiguration().getAppender(name);
         final List<String> events = listApp.getMessages();
-        assertThat(events, hasSize(1));
-        final String actual = events.get(0);
-        assertThat(actual, equalTo(expected));
+        assertThat(events)
+                .hasSize(1)
+                .containsExactly(expected);
         listApp.clear();
     }
 
diff --git a/log4j-jcl/src/test/resources/org/apache/logging/log4j/jcl/CallerInformationTest.xml b/log4j-jcl/src/test/resources/org/apache/logging/log4j/jcl/CallerInformationTest.xml
new file mode 100644
index 0000000000..997aaa7049
--- /dev/null
+++ b/log4j-jcl/src/test/resources/org/apache/logging/log4j/jcl/CallerInformationTest.xml
@@ -0,0 +1,42 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<Configuration name="CallerInformationTest" status="OFF">
+    <Appenders>
+        <List name="Class">
+            <PatternLayout pattern="%class"/>
+        </List>
+        <List name="Method">
+            <PatternLayout pattern="%method"/>
+        </List>
+        <List name="Fqcn">
+            <PatternLayout pattern="%fqcn"/>
+        </List>
+    </Appenders>
+    <Loggers>
+        <Logger name="ClassLogger" level="info">
+            <AppenderRef ref="Class"/>
+        </Logger>
+        <Logger name="MethodLogger" level="info">
+            <AppenderRef ref="Method"/>
+        </Logger>
+        <Logger name="FqcnLogger" level="info">
+            <AppenderRef ref="Fqcn"/>
+        </Logger>
+        <Root level="off"/>
+    </Loggers>
+</Configuration>
diff --git a/log4j-jcl/src/test/resources/log4j-test1.xml b/log4j-jcl/src/test/resources/org/apache/logging/log4j/jcl/LoggerTest.xml
similarity index 100%
rename from log4j-jcl/src/test/resources/log4j-test1.xml
rename to log4j-jcl/src/test/resources/org/apache/logging/log4j/jcl/LoggerTest.xml
diff --git a/src/changelog/.2.x.x/1865_fix_apache_commons_logging.xml b/src/changelog/.2.x.x/1865_fix_apache_commons_logging.xml
new file mode 100644
index 0000000000..2a180144b5
--- /dev/null
+++ b/src/changelog/.2.x.x/1865_fix_apache_commons_logging.xml
@@ -0,0 +1,27 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+       xmlns="http://logging.apache.org/log4j/changelog"
+       xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
+       type="fixed">
+  <issue id="1865" link="https://github.com/apache/logging-log4j2/issues/1865"/>
+  <author id="github:ppkarwasz"/>
+  <description format="asciidoc">
+    Fixes the Apache Commons Logging (JCL) bridge: `log4j-jcl`.
+  </description>
+</entry>
diff --git a/src/site/_release-notes.adoc b/src/site/_release-notes.adoc
index c4743cb131..b2f942c016 100644
--- a/src/site/_release-notes.adoc
+++ b/src/site/_release-notes.adoc
@@ -36,6 +36,7 @@
 [#release-notes]
 = Release Notes
 
+include::_release-notes/_2.x.x.adoc[]
 include::_release-notes/_2.21.0.adoc[]
 include::_release-notes/_2.20.0.adoc[]
 include::_release-notes/_2.19.0.adoc[]
diff --git a/src/site/_release-notes/_2.x.x.adoc b/src/site/_release-notes/_2.x.x.adoc
index 8e28472b1e..69602b9e49 100644
--- a/src/site/_release-notes/_2.x.x.adoc
+++ b/src/site/_release-notes/_2.x.x.adoc
@@ -43,3 +43,7 @@ All packages marked as private in the Javadoc are not exported.
 
 The module name of four bridges (`log4j-slf4j-impl`, `log4j-slf4j2-impl`, `log4j-to-jul` and `log4j-to-slf4j`) have been changed to adhere to the same convention as the OSGi bundle names.
 
+
+=== Fixed
+
+* Fixes the Apache Commons Logging (JCL) bridge: `log4j-jcl`. (https://github.com/apache/logging-log4j2/issues/1865[1865])