Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -119,13 +119,21 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem {
private final LoggerContext loggerContext;

/**
* Create a new {@link Log4J2LoggingSystem} instance.
* Create a new {@link Log4J2LoggingSystem} instance. The loggerContext is
* instantiated internally in the class from
* {@link LogManager#getContext(ClassLoader, boolean)} <br/>
* This constructor is also intended to be used by
* <code>LoggingSystem.get(ClassLoader, String)</code> with reflection.
* @param classLoader the class loader to use.
* @param loggerContext the {@link LoggerContext} to use.
* @throws IllegalArgumentException if the loggerContext instantiated internally is
* not of type org.apache.logging.log4j.core.LoggerContext
Comment on lines -122 to +129
Copy link
Member

@wilkinsona wilkinsona Mar 2, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think these changes add much, particular not the part that's describing what the code does. We generally reserve comments for situations where we need to describe why something is the way it is. The code itself describes the what. Please revert. This will leave this constructor more closely aligned with the other logging system constructors.

*/
Log4J2LoggingSystem(ClassLoader classLoader, LoggerContext loggerContext) {
Log4J2LoggingSystem(ClassLoader classLoader) {
super(classLoader);
this.loggerContext = loggerContext;
org.apache.logging.log4j.spi.LoggerContext spiLoggerContext = LogManager.getContext(classLoader, false);
Assert.isInstanceOf(LoggerContext.class, spiLoggerContext,
"Log4j2LoggingSystem requires LoggerContext to be of type org.apache.logging.log4j.core.LoggerContext");
this.loggerContext = (LoggerContext) spiLoggerContext;
}

@Override
Expand Down Expand Up @@ -528,10 +536,11 @@ public static class Factory implements LoggingSystemFactory {
@Override
public @Nullable LoggingSystem getLoggingSystem(ClassLoader classLoader) {
if (PRESENT) {
org.apache.logging.log4j.spi.LoggerContext spiLoggerContext = LogManager.getContext(classLoader, false);
Assert.state(spiLoggerContext instanceof LoggerContext, "");
if (spiLoggerContext instanceof LoggerContext coreLoggerContext) {
return new Log4J2LoggingSystem(classLoader, coreLoggerContext);
try {
return new Log4J2LoggingSystem(classLoader);
}
catch (IllegalStateException ex) {
// Continue
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ void loggingSystemCanBeDisabled() {
assertThat(loggingSystem).isInstanceOf(NoOpLoggingSystem.class);
}

@Test
void log4j2CanBeForcedUsingSystemProperty() {
System.setProperty(LoggingSystem.SYSTEM_PROPERTY, Log4J2LoggingSystem.class.getName());
assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(Log4J2LoggingSystem.class);
}

@Test
void julj2CanBeForcedUsingSystemProperty() {
System.setProperty(LoggingSystem.SYSTEM_PROPERTY, JavaLoggingSystem.class.getName());
assertThat(LoggingSystem.get(getClass().getClassLoader())).isInstanceOf(JavaLoggingSystem.class);
}

@Test
void getLoggerConfigurationIsUnsupported() {
assertThatExceptionOfType(UnsupportedOperationException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ void cleanUp() {
this.loggingSystem.getConfiguration().stop();
this.loggingSystem.cleanUp();
PluginRegistry.getInstance().clear();
LogManager.getFactory().removeContext(this.loggingSystem.getLoggerContext());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

}

@Test
Expand All @@ -127,7 +128,7 @@ void noFile(CapturedOutput output) {
Configuration configuration = this.loggingSystem.getConfiguration();
assertThat(output).contains("Hello world").doesNotContain("Hidden");
assertThat(new File(tmpDir() + "/spring.log")).doesNotExist();
assertThat(configuration.getConfigurationSource().getFile()).isNotNull();
assertThat(configuration.getConfigurationSource().getLocation()).contains("log4j2-test.xml");
Comment on lines -130 to +131
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change appears to be unrelated. Please revert

}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

package org.springframework.boot.logging.log4j2;

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.jspecify.annotations.Nullable;

Expand All @@ -26,7 +25,8 @@ class TestLog4J2LoggingSystem extends Log4J2LoggingSystem {

TestLog4J2LoggingSystem(String contextName) {
// Tests add resources to the thread context classloader
super(Thread.currentThread().getContextClassLoader(), new LoggerContext(contextName));
super(Thread.currentThread().getContextClassLoader());
getLoggerContext().setName(contextName);
getLoggerContext().start();
}

Expand Down
Loading