Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import org.apache.logging.log4j.message.SimpleMessage;
import org.apache.logging.log4j.message.StringFormatterMessageFactory;
import org.apache.logging.log4j.message.StructuredDataMessage;
import org.apache.logging.log4j.status.StatusLogger;
import org.awaitility.Awaitility;
import org.hamcrest.MatcherAssert;
import org.junit.jupiter.api.Tag;
Expand Down Expand Up @@ -121,7 +122,7 @@ public void basicFlow() {

@Test
public void builder() {
final int currentLine = 124;
final int currentLine = 125;
logger.atDebug().withLocation().log("Hello");
final Marker marker = MarkerManager.getMarker("test");
logger.atError().withMarker(marker).log("Hello {}", "John");
Expand Down Expand Up @@ -419,6 +420,17 @@ public void getLogger_String_MessageFactoryMismatchNull(final TestInfo testInfo)
events.get(0).getMessage().getFormattedMessage());
}

@Test
public void getLogger_String_MessageFactoryMismatchProducesNoWarning(final TestInfo testInfo) {
final String name = testInfo.getTestMethod().map(Method::getName).orElseThrow(AssertionError::new);
testMessageFactoryMismatch(name, StringFormatterMessageFactory.INSTANCE, new ReusableMessageFactory());
testMessageFactoryMismatch(name + "Null", StringFormatterMessageFactory.INSTANCE, null);
final boolean mismatchWarning = StatusLogger.getLogger().getStatusData().stream()
.map(data -> data.getMessage().getFormattedMessage())
.anyMatch(message -> message.contains("created with the message factory"));
assertFalse(mismatchWarning, "The message factory mismatch warning should not be emitted");
}

@Test
public void mdc() {
ThreadContext.put("TestYear", "2010");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,11 @@
import org.apache.logging.log4j.plugins.di.Key;
import org.apache.logging.log4j.plugins.di.spi.ConfigurableInstanceFactoryPostProcessor;
import org.apache.logging.log4j.plugins.util.OrderedComparator;
import org.apache.logging.log4j.spi.ExtendedLogger;
import org.apache.logging.log4j.spi.LoggerContextFactory;
import org.apache.logging.log4j.spi.LoggerContextShutdownAware;
import org.apache.logging.log4j.spi.LoggerContextShutdownEnabled;
import org.apache.logging.log4j.spi.LoggerRegistry;
import org.apache.logging.log4j.spi.Terminable;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.Lazy;
import org.apache.logging.log4j.util.ServiceLoaderUtil;
import org.jspecify.annotations.Nullable;
Expand Down Expand Up @@ -182,38 +180,6 @@ public LoggerContext(
this(contextName, externalContext, fileToUri(configLocation), instanceFactory);
}

/**
* Checks that the message factory a logger was created with is the same as the given messageFactory. If they are
* different log a warning to the {@linkplain StatusLogger}. A null MessageFactory translates to the default
* MessageFactory.
*
* @param logger The logger to check
* @param messageFactory The message factory to check.
*/
private void checkMessageFactory(final ExtendedLogger logger, final MessageFactory messageFactory) {
final String name = logger.getName();
final MessageFactory loggerMessageFactory = logger.getMessageFactory();
final MessageFactory currentMessageFactory = defaultMessageFactory;
if (messageFactory != null && !loggerMessageFactory.equals(messageFactory)) {
StatusLogger.getLogger()
.warn(
"The Logger {} was created with the message factory {} and is now requested with the "
+ "message factory {}, which may create log events with unexpected formatting.",
name,
loggerMessageFactory,
messageFactory);
} else if (messageFactory == null && loggerMessageFactory != currentMessageFactory) {
StatusLogger.getLogger()
.warn(
"The Logger {} was created with the message factory {} and is now requested with a null "
+ "message factory (defaults to {}), which may create log events with unexpected "
+ "formatting.",
name,
loggerMessageFactory,
currentMessageFactory.getClass().getName());
}
}

public PropertyEnvironment getEnvironment() {
return environment;
}
Expand Down Expand Up @@ -563,19 +529,16 @@ public Collection<Logger> getLoggers() {
}

/**
* Obtains a Logger from the Context.
* Obtains a logger from the context.
*
* @param name The name of the Logger to return.
* @param messageFactory The message factory is used only when creating a logger, subsequent use does not change the
* logger but will log a warning if mismatched.
* @return The Logger.
* @param name a logger name
* @param messageFactory a message factory to associate the logger with
* @return a logger matching the given name and message factory
*/
@Override
public Logger getLogger(final String name, final MessageFactory messageFactory) {
public Logger getLogger(final String name, final @Nullable MessageFactory messageFactory) {
final MessageFactory actualMessageFactory = messageFactory != null ? messageFactory : defaultMessageFactory;
final Logger logger = loggerRegistry.computeIfAbsent(name, actualMessageFactory, this::newLogger);
checkMessageFactory(logger, actualMessageFactory);
return logger;
return loggerRegistry.computeIfAbsent(name, actualMessageFactory, this::newLogger);
}

/**
Expand Down Expand Up @@ -617,7 +580,7 @@ public boolean hasLogger(final String name) {
* @return True if the Logger exists, false otherwise.
*/
@Override
public boolean hasLogger(final String name, final MessageFactory messageFactory) {
public boolean hasLogger(final String name, final @Nullable MessageFactory messageFactory) {
return loggerRegistry.hasLogger(name, messageFactory);
}

Expand Down