From ffd1ab273551a7df537693f6071afbd184a6a9cc Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Tue, 3 Jun 2025 08:48:36 +0200 Subject: [PATCH 1/2] Fixes OSGi descriptor to accept Disruptor 4 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Log4j Core added support for Disruptor 4 in `2.23.0` (see #1829), but we forgot to update the OSGi metadata to allow Disruptor 4 in that environment. This PR expands the accepted Disruptor version range in the OSGi descriptor to ``[3.4,5)``, which partially addresses #3706. I've also added a small test to try and reproduce the issue from #3706 (where Disruptor version 3 is misidentified as Disruptor 4), and to confirm that async logging works in an OSGi setup. Unfortunately, we couldn't reproduce the bug in our test environment—so that part still needs investigation. However, the version range issue is separate and should now be resolved by this change. --- log4j-core/pom.xml | 3 +- log4j-osgi-test/pom.xml | 47 +++++++++++ .../log4j/osgi/tests/DisruptorTest.java | 84 +++++++++++++++++++ src/changelog/.2.x.x/3706_osgi-disruptor.xml | 8 ++ .../2.23.0/add_support_for_disruptor_4.xml | 2 +- 5 files changed, 142 insertions(+), 2 deletions(-) create mode 100644 log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/DisruptorTest.java create mode 100644 src/changelog/.2.x.x/3706_osgi-disruptor.xml diff --git a/log4j-core/pom.xml b/log4j-core/pom.xml index f5693dc2e92..49293f47dfc 100644 --- a/log4j-core/pom.xml +++ b/log4j-core/pom.xml @@ -51,6 +51,7 @@ + [3.4,5) true @@ -58,7 +59,7 @@ com.conversantmedia.util.concurrent;resolution:=optional; com.fasterxml.jackson.*;resolution:=optional, - com.lmax.disruptor.*;resolution:=optional, + com.lmax.disruptor.*;version="${disruptor.support.range}";resolution:=optional, javax.activation;resolution:=optional, javax.jms;version="[1.1,3)";resolution:=optional, javax.mail.*;version="[1.6,2)";resolution:=optional, diff --git a/log4j-osgi-test/pom.xml b/log4j-osgi-test/pom.xml index a4f3d911797..02410a272a8 100644 --- a/log4j-osgi-test/pom.xml +++ b/log4j-osgi-test/pom.xml @@ -86,6 +86,11 @@ log4j-to-slf4j test + + com.lmax + disruptor + test + org.junit.jupiter junit-jupiter-engine @@ -233,9 +238,30 @@ org.apache.logging.log4j.osgi.tests.FelixLoadApiBundleTest + org.apache.logging.log4j.osgi.tests.DisruptorTest + + test-equinox-disruptor + + test + + + + + com.lmax.disruptor:disruptor + org.apache.felix:org.apache.felix.framework + + + org.apache.logging.log4j.osgi.tests.DisruptorTest + + + org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector + + + + test-felix @@ -247,9 +273,30 @@ org.apache.logging.log4j.osgi.tests.EquinoxLoadApiBundleTest + org.apache.logging.log4j.osgi.tests.DisruptorTest + + test-felix-disruptor + + test + + + + + com.lmax.disruptor:disruptor + org.eclipse.platform:org.eclipse.osgi + + + org.apache.logging.log4j.osgi.tests.DisruptorTest + + + org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector + + + + diff --git a/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/DisruptorTest.java b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/DisruptorTest.java new file mode 100644 index 00000000000..45ce5fef61c --- /dev/null +++ b/log4j-osgi-test/src/test/java/org/apache/logging/log4j/osgi/tests/DisruptorTest.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.log4j.osgi.tests; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.ops4j.pax.exam.CoreOptions.junitBundles; +import static org.ops4j.pax.exam.CoreOptions.linkBundle; +import static org.ops4j.pax.exam.CoreOptions.options; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.LoggerContext; +import org.apache.logging.log4j.core.async.AsyncLoggerContext; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.ops4j.pax.exam.Option; +import org.ops4j.pax.exam.junit.PaxExam; +import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy; +import org.ops4j.pax.exam.spi.reactors.PerClass; + +@RunWith(PaxExam.class) +@ExamReactorStrategy(PerClass.class) +public class DisruptorTest { + + @org.ops4j.pax.exam.Configuration + public Option[] config() { + return options( + linkBundle("org.apache.logging.log4j.api"), + linkBundle("org.apache.logging.log4j.core"), + linkBundle("com.lmax.disruptor"), + // 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"), + junitBundles()); + } + + @Test + public void testDisruptorLog() { + // Logger context + LoggerContext context = getLoggerContext(); + assertTrue("LoggerContext is an instance of AsyncLoggerContext", context instanceof AsyncLoggerContext); + final CustomConfiguration custom = (CustomConfiguration) context.getConfiguration(); + // Logging + final Logger logger = LogManager.getLogger(getClass()); + logger.info("Hello OSGI from Log4j2!"); + + context.stop(); + assertEquals(1, custom.getEvents().size()); + final LogEvent event = custom.getEvents().get(0); + assertEquals("Hello OSGI from Log4j2!", event.getMessage().getFormattedMessage()); + assertEquals(Level.INFO, event.getLevel()); + custom.clearEvents(); + } + + private static LoggerContext getLoggerContext() { + final LoggerContext ctx = (LoggerContext) LogManager.getContext(false); + assertEquals("AsyncDefault", ctx.getName()); + return ctx; + } +} diff --git a/src/changelog/.2.x.x/3706_osgi-disruptor.xml b/src/changelog/.2.x.x/3706_osgi-disruptor.xml new file mode 100644 index 00000000000..a6cf0a7c333 --- /dev/null +++ b/src/changelog/.2.x.x/3706_osgi-disruptor.xml @@ -0,0 +1,8 @@ + + + + Fixes OSGi descriptor to accept Disruptor 4. + diff --git a/src/changelog/2.23.0/add_support_for_disruptor_4.xml b/src/changelog/2.23.0/add_support_for_disruptor_4.xml index 23ef1f58753..34ec5b45e95 100644 --- a/src/changelog/2.23.0/add_support_for_disruptor_4.xml +++ b/src/changelog/2.23.0/add_support_for_disruptor_4.xml @@ -3,6 +3,6 @@ xmlns="https://logging.apache.org/xml/ns" xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" type="added"> - + Added support for LMAX Disruptor 4.x From 82354cc4cc608e9d118c283f390e8a7add643554 Mon Sep 17 00:00:00 2001 From: "Piotr P. Karwasz" Date: Tue, 3 Jun 2025 09:05:53 +0200 Subject: [PATCH 2/2] fix: adjust formatting --- log4j-osgi-test/pom.xml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/log4j-osgi-test/pom.xml b/log4j-osgi-test/pom.xml index 02410a272a8..b63666715e0 100644 --- a/log4j-osgi-test/pom.xml +++ b/log4j-osgi-test/pom.xml @@ -257,8 +257,7 @@ org.apache.logging.log4j.osgi.tests.DisruptorTest - org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector - + org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector @@ -292,8 +291,7 @@ org.apache.logging.log4j.osgi.tests.DisruptorTest - org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector - + org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector