Skip to content

Commit 69b3058

Browse files
authored
Fixes OSGi descriptor to accept Disruptor 4 (#3707)
* Fixes OSGi descriptor to accept Disruptor 4 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. * fix: adjust formatting
1 parent 49cdf74 commit 69b3058

File tree

5 files changed

+140
-2
lines changed

5 files changed

+140
-2
lines changed

log4j-core/pom.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,15 @@
5151
<!--
5252
~ OSGi and JPMS options
5353
-->
54+
<disruptor.support.range>[3.4,5)</disruptor.support.range>
5455
<bnd-multi-release>true</bnd-multi-release>
5556
<bnd-extra-package-options>
5657
<!-- Annotations only -->
5758
org.jspecify.*;resolution:=optional,
5859
<!-- External optional dependencies -->
5960
com.conversantmedia.util.concurrent;resolution:=optional;
6061
com.fasterxml.jackson.*;resolution:=optional,
61-
com.lmax.disruptor.*;resolution:=optional,
62+
com.lmax.disruptor.*;version="${disruptor.support.range}";resolution:=optional,
6263
javax.activation;resolution:=optional,
6364
javax.jms;version="[1.1,3)";resolution:=optional,
6465
javax.mail.*;version="[1.6,2)";resolution:=optional,

log4j-osgi-test/pom.xml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@
8686
<artifactId>log4j-to-slf4j</artifactId>
8787
<scope>test</scope>
8888
</dependency>
89+
<dependency>
90+
<groupId>com.lmax</groupId>
91+
<artifactId>disruptor</artifactId>
92+
<scope>test</scope>
93+
</dependency>
8994
<dependency>
9095
<groupId>org.junit.jupiter</groupId>
9196
<artifactId>junit-jupiter-engine</artifactId>
@@ -233,9 +238,29 @@
233238
</classpathDependencyExcludes>
234239
<excludes>
235240
<exclude>org.apache.logging.log4j.osgi.tests.FelixLoadApiBundleTest</exclude>
241+
<exclude>org.apache.logging.log4j.osgi.tests.DisruptorTest</exclude>
236242
</excludes>
237243
</configuration>
238244
</execution>
245+
<execution>
246+
<id>test-equinox-disruptor</id>
247+
<goals>
248+
<goal>test</goal>
249+
</goals>
250+
<configuration>
251+
<classpathDependencyExcludes combine.children="append">
252+
<!-- Ensure that Disruptor is not accidentally loaded from the parent classpath -->
253+
<exclude>com.lmax.disruptor:disruptor</exclude>
254+
<exclude>org.apache.felix:org.apache.felix.framework</exclude>
255+
</classpathDependencyExcludes>
256+
<includes>
257+
<include>org.apache.logging.log4j.osgi.tests.DisruptorTest</include>
258+
</includes>
259+
<systemPropertyVariables>
260+
<log4j2.contextSelector>org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector</log4j2.contextSelector>
261+
</systemPropertyVariables>
262+
</configuration>
263+
</execution>
239264
<execution>
240265
<id>test-felix</id>
241266
<goals>
@@ -247,9 +272,29 @@
247272
</classpathDependencyExcludes>
248273
<excludes>
249274
<exclude>org.apache.logging.log4j.osgi.tests.EquinoxLoadApiBundleTest</exclude>
275+
<exclude>org.apache.logging.log4j.osgi.tests.DisruptorTest</exclude>
250276
</excludes>
251277
</configuration>
252278
</execution>
279+
<execution>
280+
<id>test-felix-disruptor</id>
281+
<goals>
282+
<goal>test</goal>
283+
</goals>
284+
<configuration>
285+
<classpathDependencyExcludes combine.children="append">
286+
<!-- Ensure that Disruptor is not accidentally loaded from the parent classpath -->
287+
<exclude>com.lmax.disruptor:disruptor</exclude>
288+
<exclude>org.eclipse.platform:org.eclipse.osgi</exclude>
289+
</classpathDependencyExcludes>
290+
<includes>
291+
<include>org.apache.logging.log4j.osgi.tests.DisruptorTest</include>
292+
</includes>
293+
<systemPropertyVariables>
294+
<log4j2.contextSelector>org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector</log4j2.contextSelector>
295+
</systemPropertyVariables>
296+
</configuration>
297+
</execution>
253298
</executions>
254299
</plugin>
255300

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to you under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.logging.log4j.osgi.tests;
18+
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertTrue;
21+
import static org.ops4j.pax.exam.CoreOptions.junitBundles;
22+
import static org.ops4j.pax.exam.CoreOptions.linkBundle;
23+
import static org.ops4j.pax.exam.CoreOptions.options;
24+
25+
import org.apache.logging.log4j.Level;
26+
import org.apache.logging.log4j.LogManager;
27+
import org.apache.logging.log4j.Logger;
28+
import org.apache.logging.log4j.core.LogEvent;
29+
import org.apache.logging.log4j.core.LoggerContext;
30+
import org.apache.logging.log4j.core.async.AsyncLoggerContext;
31+
import org.junit.Test;
32+
import org.junit.runner.RunWith;
33+
import org.ops4j.pax.exam.Option;
34+
import org.ops4j.pax.exam.junit.PaxExam;
35+
import org.ops4j.pax.exam.spi.reactors.ExamReactorStrategy;
36+
import org.ops4j.pax.exam.spi.reactors.PerClass;
37+
38+
@RunWith(PaxExam.class)
39+
@ExamReactorStrategy(PerClass.class)
40+
public class DisruptorTest {
41+
42+
@org.ops4j.pax.exam.Configuration
43+
public Option[] config() {
44+
return options(
45+
linkBundle("org.apache.logging.log4j.api"),
46+
linkBundle("org.apache.logging.log4j.core"),
47+
linkBundle("com.lmax.disruptor"),
48+
// required by Pax Exam's logging
49+
linkBundle("org.objectweb.asm"),
50+
linkBundle("org.objectweb.asm.commons"),
51+
linkBundle("org.objectweb.asm.tree"),
52+
linkBundle("org.objectweb.asm.tree.analysis"),
53+
linkBundle("org.objectweb.asm.util"),
54+
linkBundle("org.apache.aries.spifly.dynamic.bundle").startLevel(2),
55+
linkBundle("slf4j.api"),
56+
linkBundle("ch.qos.logback.classic"),
57+
linkBundle("ch.qos.logback.core"),
58+
junitBundles());
59+
}
60+
61+
@Test
62+
public void testDisruptorLog() {
63+
// Logger context
64+
LoggerContext context = getLoggerContext();
65+
assertTrue("LoggerContext is an instance of AsyncLoggerContext", context instanceof AsyncLoggerContext);
66+
final CustomConfiguration custom = (CustomConfiguration) context.getConfiguration();
67+
// Logging
68+
final Logger logger = LogManager.getLogger(getClass());
69+
logger.info("Hello OSGI from Log4j2!");
70+
71+
context.stop();
72+
assertEquals(1, custom.getEvents().size());
73+
final LogEvent event = custom.getEvents().get(0);
74+
assertEquals("Hello OSGI from Log4j2!", event.getMessage().getFormattedMessage());
75+
assertEquals(Level.INFO, event.getLevel());
76+
custom.clearEvents();
77+
}
78+
79+
private static LoggerContext getLoggerContext() {
80+
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
81+
assertEquals("AsyncDefault", ctx.getName());
82+
return ctx;
83+
}
84+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="https://logging.apache.org/xml/ns"
4+
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
5+
type="fixed">
6+
<issue id="3706" link="https://github.com/apache/logging-log4j2/issues/3706"/>
7+
<description format="asciidoc">Fixes OSGi descriptor to accept Disruptor 4.</description>
8+
</entry>

src/changelog/2.23.0/add_support_for_disruptor_4.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@
33
xmlns="https://logging.apache.org/xml/ns"
44
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd"
55
type="added">
6-
<issue id="1821" link="https://github.com/apache/logging-log4j2/pull/1821"/>
6+
<issue id="1829" link="https://github.com/apache/logging-log4j2/issues/1829"/>
77
<description format="asciidoc">Added support for LMAX Disruptor 4.x</description>
88
</entry>

0 commit comments

Comments
 (0)