Skip to content

Commit

Permalink
Port log4j-jcl changes from 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
vy committed Jan 10, 2024
1 parent 4bd7e19 commit a863232
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 55 deletions.
15 changes: 6 additions & 9 deletions log4j-jcl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,47 @@
*/
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!");
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 @@ -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();
}
}
42 changes: 42 additions & 0 deletions log4j-jcl/src/test/resources/CallerInformationTest.xml
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>
File renamed without changes.

0 comments on commit a863232

Please sign in to comment.