This is an automated email from the ASF dual-hosted git repository.
vy pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/main by this push:
new a86323223f Port `log4j-jcl` changes from `2.x`
a86323223f is described below
commit a86323223f4a04018d2b4d859d232bd2283a5da3
Author: Volkan Yazıcı <[email protected]>
AuthorDate: Wed Jan 10 14:00:24 2024 +0100
Port `log4j-jcl` changes from `2.x`
---
log4j-jcl/pom.xml | 15 +++-----
.../logging/log4j/jcl/CallerInformationTest.java | 40 +++++++++----------
.../org/apache/logging/log4j/jcl/LoggerTest.java | 45 ++++++++++------------
.../src/test/resources/CallerInformationTest.xml | 42 ++++++++++++++++++++
.../resources/{log4j-test1.xml => LoggerTest.xml} | 0
5 files changed, 87 insertions(+), 55 deletions(-)
diff --git a/log4j-jcl/pom.xml b/log4j-jcl/pom.xml
index 75f4225347..acc7c8d8a3 100644
--- a/log4j-jcl/pom.xml
+++ b/log4j-jcl/pom.xml
@@ -23,16 +23,14 @@
<version>${revision}</version>
<relativePath>../log4j-parent</relativePath>
</parent>
-
<artifactId>log4j-jcl</artifactId>
<packaging>jar</packaging>
<name>Apache Log4j Commons Logging Bridge</name>
<description>The Apache Log4j Commons Logging Adapter</description>
<properties>
- <log4jParentDir>${basedir}/..</log4jParentDir>
<bnd-extra-module-options>
- <!-- Use module name returned by `jar -d` -->
- commons.logging;substitute="commons-logging"
+ <!-- Filebase module names: MUST be static -->
+ commons.logging;substitute="commons-logging";transitive=false;static=true
</bnd-extra-module-options>
</properties>
<dependencies>
@@ -60,15 +58,14 @@
<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>
-
</project>
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 6e3f3dba40..7bdbe3baf3 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
@@ -16,41 +16,40 @@
*/
package org.apache.logging.log4j.jcl;
-import static org.junit.Assert.assertEquals;
+import static org.assertj.core.api.Assertions.assertThat;
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.core.test.junit.LoggerContextSource;
+import org.apache.logging.log4j.test.junit.SetTestProperty;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
+@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();
+ @LoggerContextSource("CallerInformationTest.xml")
+ public void testClassLogger(final LoggerContext ctx) {
+ 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();
+ @LoggerContextSource("CallerInformationTest.xml")
+ public void testMethodLogger(final LoggerContext ctx) {
+ final ListAppender app = ctx.getConfiguration().getAppender("Method");
+ app.clear();
final Log logger = LogFactory.getLog("MethodLogger");
logger.info("More messages.");
logger.warn("CATASTROPHE INCOMING!");
@@ -58,9 +57,6 @@ 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 3f096fb93f..0c4fc4f3bf 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
@@ -16,48 +16,45 @@
*/
package org.apache.logging.log4j.jcl;
-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;
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.core.test.junit.LoggerContextSource;
+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;
-/**
- *
- */
-public class LoggerTest {
-
- private static final String CONFIG = "log4j-test1.xml";
+@UsingStatusListener
+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() {
+ @LoggerContextSource("LoggerTest.xml")
+ void testLog(final LoggerContext loggerContext) {
final Log logger = LogFactory.getLog("LoggerTest");
logger.debug("Test message");
- verify("List", "o.a.l.l.j.LoggerTest Test message MDC{}" +
Strings.LINE_SEPARATOR);
+ verify(loggerContext, "o.a.l.l.j.LoggerTest Test message MDC{}" +
Strings.LINE_SEPARATOR);
logger.debug("Exception: ", new NullPointerException("Test"));
- verify("List", "o.a.l.l.j.LoggerTest Exception: MDC{}" +
Strings.LINE_SEPARATOR);
+ verify(loggerContext, "o.a.l.l.j.LoggerTest Exception: MDC{}" +
Strings.LINE_SEPARATOR);
logger.info("Info Message");
- verify("List", "o.a.l.l.j.LoggerTest Info Message MDC{}" +
Strings.LINE_SEPARATOR);
+ verify(loggerContext, "o.a.l.l.j.LoggerTest Info Message MDC{}" +
Strings.LINE_SEPARATOR);
logger.info("Info Message {}");
- verify("List", "o.a.l.l.j.LoggerTest Info Message {} MDC{}" +
Strings.LINE_SEPARATOR);
+ verify(loggerContext, "o.a.l.l.j.LoggerTest Info Message {} MDC{}" +
Strings.LINE_SEPARATOR);
}
- private void verify(final String name, final String expected) {
- final ListAppender listApp = context.getListAppender(name);
+ private static void verify(final LoggerContext loggerContext, final String
expected) {
+ final ListAppender listApp =
loggerContext.getConfiguration().getAppender("List");
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/CallerInformationTest.xml
b/log4j-jcl/src/test/resources/CallerInformationTest.xml
new file mode 100644
index 0000000000..997aaa7049
--- /dev/null
+++ b/log4j-jcl/src/test/resources/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/LoggerTest.xml
similarity index 100%
rename from log4j-jcl/src/test/resources/log4j-test1.xml
rename to log4j-jcl/src/test/resources/LoggerTest.xml