Skip to content

Commit 232f3b0

Browse files
committed
Fix JCL to Log4j API bridge
1 parent f303f2d commit 232f3b0

File tree

9 files changed

+128
-43
lines changed

9 files changed

+128
-43
lines changed

log4j-jcl/pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@
6060
<scope>test</scope>
6161
</dependency>
6262
<dependency>
63-
<groupId>org.junit.jupiter</groupId>
64-
<artifactId>junit-jupiter-engine</artifactId>
63+
<groupId>org.assertj</groupId>
64+
<artifactId>assertj-core</artifactId>
6565
<scope>test</scope>
6666
</dependency>
6767
<dependency>
68-
<groupId>org.junit.vintage</groupId>
69-
<artifactId>junit-vintage-engine</artifactId>
68+
<groupId>org.junit.jupiter</groupId>
69+
<artifactId>junit-jupiter-api</artifactId>
7070
<scope>test</scope>
7171
</dependency>
7272
</dependencies>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
org.apache.logging.log4j.jcl.LogFactoryImpl
2+
###
3+
# ██ ██ █████ ██████ ███ ██ ██ ███ ██ ██████ ██
4+
# ██ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██ ██ ██
5+
# ██ █ ██ ███████ ██████ ██ ██ ██ ██ ██ ██ ██ ██ ███ ██
6+
# ██ ███ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██
7+
# ███ ███ ██ ██ ██ ██ ██ ████ ██ ██ ████ ██████ ██
8+
#
9+
# Apache Commons Logging 1.2 does **not** use the `ServiceLoader` class.
10+
# It reads the first line of this file and expects it to be a FQCN.
11+
#
12+
# cf. https://github.com/apache/logging-log4j2/issues/1865

log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/CallerInformationTest.java

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,48 +20,47 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
import org.apache.logging.log4j.core.LoggerContext;
2324
import org.apache.logging.log4j.core.test.appender.ListAppender;
24-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
25-
import org.junit.ClassRule;
26-
import org.junit.Test;
25+
import org.apache.logging.log4j.test.junit.SetTestProperty;
26+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
27+
import org.junit.jupiter.api.Test;
2728

28-
import static org.junit.Assert.assertEquals;
29+
import static org.assertj.core.api.Assertions.assertThat;
2930

31+
@UsingStatusListener
32+
@SetTestProperty(key ="log4j2.configurationFile", value="org/apache/logging/log4j/jcl/CallerInformationTest.xml")
3033
public class CallerInformationTest {
3134

32-
// config from log4j-core test-jar
33-
private static final String CONFIG = "log4j2-calling-class.xml";
34-
35-
@ClassRule
36-
public static final LoggerContextRule ctx = new LoggerContextRule(CONFIG);
37-
3835
@Test
3936
public void testClassLogger() throws Exception {
40-
final ListAppender app = ctx.getListAppender("Class").clear();
37+
final LoggerContext ctx = LoggerContext.getContext(false);
38+
final ListAppender app = ctx.getConfiguration().getAppender("Class");
39+
app.clear();
4140
final Log logger = LogFactory.getLog("ClassLogger");
4241
logger.info("Ignored message contents.");
4342
logger.warn("Verifying the caller class is still correct.");
4443
logger.error("Hopefully nobody breaks me!");
4544
final List<String> messages = app.getMessages();
46-
assertEquals("Incorrect number of messages.", 3, messages.size());
47-
for (final String message : messages) {
48-
assertEquals("Incorrect caller class name.", this.getClass().getName(), message);
49-
}
45+
assertThat(messages)
46+
.hasSize(3)
47+
.allMatch(c -> getClass().getName().equals(c));
5048
}
5149

5250
@Test
5351
public void testMethodLogger() throws Exception {
54-
final ListAppender app = ctx.getListAppender("Method").clear();
52+
final LoggerContext ctx = LoggerContext.getContext(false);
53+
final ListAppender app = ctx.getConfiguration().getAppender("Method");
54+
app.clear();
5555
final Log logger = LogFactory.getLog("MethodLogger");
5656
logger.info("More messages.");
5757
logger.warn("CATASTROPHE INCOMING!");
5858
logger.error("ZOMBIES!!!");
5959
logger.warn("brains~~~");
6060
logger.info("Itchy. Tasty.");
6161
final List<String> messages = app.getMessages();
62-
assertEquals("Incorrect number of messages.", 5, messages.size());
63-
for (final String message : messages) {
64-
assertEquals("Incorrect caller method name.", "testMethodLogger", message);
65-
}
62+
assertThat(messages)
63+
.hasSize(5)
64+
.allMatch("testMethodLogger"::equals);
6665
}
6766
}

log4j-jcl/src/test/java/org/apache/logging/log4j/jcl/LoggerTest.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,27 @@
2020

2121
import org.apache.commons.logging.Log;
2222
import org.apache.commons.logging.LogFactory;
23+
import org.apache.logging.log4j.core.LoggerContext;
2324
import org.apache.logging.log4j.core.test.appender.ListAppender;
24-
import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
25+
import org.apache.logging.log4j.test.junit.SetTestProperty;
26+
import org.apache.logging.log4j.test.junit.UsingStatusListener;
2527
import org.apache.logging.log4j.util.Strings;
26-
import org.junit.ClassRule;
27-
import org.junit.Test;
28+
import org.junit.jupiter.api.Test;
2829

29-
import static org.hamcrest.Matchers.equalTo;
30-
import static org.hamcrest.Matchers.hasSize;
31-
import static org.junit.Assert.*;
30+
import static org.assertj.core.api.Assertions.assertThat;
3231

33-
/**
34-
*
35-
*/
36-
public class LoggerTest {
37-
38-
private static final String CONFIG = "log4j-test1.xml";
32+
@UsingStatusListener
33+
@SetTestProperty(key ="log4j2.configurationFile", value="org/apache/logging/log4j/jcl/LoggerTest.xml")
34+
class LoggerTest {
3935

40-
@ClassRule
41-
public static final LoggerContextRule context = new LoggerContextRule(CONFIG);
36+
@Test
37+
void testFactory() {
38+
final LogFactory factory = LogFactory.getFactory();
39+
assertThat(factory).isInstanceOf(LogFactoryImpl.class);
40+
}
4241

4342
@Test
44-
public void testLog() {
43+
void testLog() {
4544
final Log logger = LogFactory.getLog("LoggerTest");
4645
logger.debug("Test message");
4746
verify("List", "o.a.l.l.j.LoggerTest Test message MDC{}" + Strings.LINE_SEPARATOR);
@@ -54,11 +53,12 @@ public void testLog() {
5453
}
5554

5655
private void verify(final String name, final String expected) {
57-
final ListAppender listApp = context.getListAppender(name);
56+
final LoggerContext context = LoggerContext.getContext(false);
57+
final ListAppender listApp = context.getConfiguration().getAppender(name);
5858
final List<String> events = listApp.getMessages();
59-
assertThat(events, hasSize(1));
60-
final String actual = events.get(0);
61-
assertThat(actual, equalTo(expected));
59+
assertThat(events)
60+
.hasSize(1)
61+
.containsExactly(expected);
6262
listApp.clear();
6363
}
6464

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration name="CallerInformationTest" status="OFF">
19+
<Appenders>
20+
<List name="Class">
21+
<PatternLayout pattern="%class"/>
22+
</List>
23+
<List name="Method">
24+
<PatternLayout pattern="%method"/>
25+
</List>
26+
<List name="Fqcn">
27+
<PatternLayout pattern="%fqcn"/>
28+
</List>
29+
</Appenders>
30+
<Loggers>
31+
<Logger name="ClassLogger" level="info">
32+
<AppenderRef ref="Class"/>
33+
</Logger>
34+
<Logger name="MethodLogger" level="info">
35+
<AppenderRef ref="Method"/>
36+
</Logger>
37+
<Logger name="FqcnLogger" level="info">
38+
<AppenderRef ref="Fqcn"/>
39+
</Logger>
40+
<Root level="off"/>
41+
</Loggers>
42+
</Configuration>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
19+
xmlns="http://logging.apache.org/log4j/changelog"
20+
xsi:schemaLocation="http://logging.apache.org/log4j/changelog https://logging.apache.org/log4j/changelog-0.1.1.xsd"
21+
type="fixed">
22+
<issue id="1865" link="https://github.com/apache/logging-log4j2/issues/1865"/>
23+
<author id="github:ppkarwasz"/>
24+
<description format="asciidoc">
25+
Fixes the Apache Commons Logging (JCL) bridge: `log4j-jcl`.
26+
</description>
27+
</entry>

src/site/_release-notes.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
[#release-notes]
3737
= Release Notes
3838
39+
include::_release-notes/_2.x.x.adoc[]
3940
include::_release-notes/_2.21.0.adoc[]
4041
include::_release-notes/_2.20.0.adoc[]
4142
include::_release-notes/_2.19.0.adoc[]

src/site/_release-notes/_2.x.x.adoc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,7 @@ All packages marked as private in the Javadoc are not exported.
4343
4444
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.
4545
46+
47+
=== Fixed
48+
49+
* Fixes the Apache Commons Logging (JCL) bridge: `log4j-jcl`. (https://github.com/apache/logging-log4j2/issues/1865[1865])

0 commit comments

Comments
 (0)