Skip to content

Commit

Permalink
Fix JCL to Log4j API bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
ppkarwasz committed Oct 20, 2023
1 parent f303f2d commit 232f3b0
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 43 deletions.
8 changes: 4 additions & 4 deletions log4j-jcl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,47 @@

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!");
logger.error("ZOMBIES!!!");
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,28 +20,27 @@

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);
Expand All @@ -54,11 +53,12 @@ public void testLog() {
}

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();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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>
27 changes: 27 additions & 0 deletions src/changelog/.2.x.x/1865_fix_apache_commons_logging.xml
Original file line number Diff line number Diff line change
@@ -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>
1 change: 1 addition & 0 deletions src/site/_release-notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
4 changes: 4 additions & 0 deletions src/site/_release-notes/_2.x.x.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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])

0 comments on commit 232f3b0

Please sign in to comment.