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 @@ -37,6 +37,7 @@
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.DefaultConfiguration;
Expand All @@ -59,7 +60,7 @@ final class Log4j2LogEventUtil {

private Log4j2LogEventUtil() {}

static LogEvent toLog4jLogEvent(String loggerName, LogData logData) {
static LogEvent toLog4jLogEvent(Logger logger, LogData logData) {
MetadataProcessor metadata =
MetadataProcessor.forScopeAndLogSite(Platform.getInjectedMetadata(), logData.getMetadata());

Expand All @@ -78,7 +79,7 @@ static LogEvent toLog4jLogEvent(String loggerName, LogData logData) {
* which can perhaps be installed as default if nothing else is present. Then, we would not rely
* on Log4j2 internals.
*/
LoggerContext ctx = LoggerContext.getContext(false);
Copy link
Author

@JerryShea JerryShea Aug 10, 2025

Choose a reason for hiding this comment

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

this can be very expensive

LoggerContext ctx = logger.getContext();
Configuration config = ctx.getConfiguration();
String message;
if (config instanceof DefaultConfiguration) {
Expand All @@ -89,14 +90,14 @@ static LogEvent toLog4jLogEvent(String loggerName, LogData logData) {
}

Throwable thrown = metadata.getSingleValue(LogContext.Key.LOG_CAUSE);
return toLog4jLogEvent(loggerName, logData, message, toLog4jLevel(logData.getLevel()), thrown);
return toLog4jLogEvent(logger.getName(), logData, message, toLog4jLevel(logData.getLevel()), thrown);
}

static LogEvent toLog4jLogEvent(String loggerName, RuntimeException error, LogData badData) {
static LogEvent toLog4jLogEvent(Logger logger, RuntimeException error, LogData badData) {
String message = formatBadLogData(error, badData);
// Re-target this log message as a warning (or above) since it indicates a real bug.
Level level = badData.getLevel().intValue() < WARNING.intValue() ? WARNING : badData.getLevel();
return toLog4jLogEvent(loggerName, badData, message, toLog4jLevel(level), error);
return toLog4jLogEvent(logger.getName(), badData, message, toLog4jLevel(level), error);
}

private static LogEvent toLog4jLogEvent(
Expand Down Expand Up @@ -246,13 +247,19 @@ private static StringMap createContextMap(LogData logData) {
MetadataProcessor metadataProcessor =
MetadataProcessor.forScopeAndLogSite(Platform.getInjectedMetadata(), logData.getMetadata());

StringMap contextData = ContextDataFactory.createContextData(metadataProcessor.keyCount());
metadataProcessor.process(
HANDLER,
(key, value) ->
contextData.putValue(key, ValueQueue.maybeWrap(value, contextData.getValue(key))));
final StringMap contextData;
// don't allocate for the common case of no keys
if (metadataProcessor.keyCount() > 0) {
contextData = ContextDataFactory.createContextData(metadataProcessor.keyCount());
metadataProcessor.process(
HANDLER,
(key, value) ->
contextData.putValue(key, ValueQueue.maybeWrap(value, contextData.getValue(key))));

contextData.freeze();
contextData.freeze();
} else {
contextData = ContextDataFactory.emptyFrozenContextData();
}

return contextData;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public boolean isLoggable(java.util.logging.Level level) {
public void log(LogData logData) {
// The caller is responsible to call isLoggable() before calling this method to ensure that only
// messages above the given threshold are logged.
logger.get().log(toLog4jLogEvent(logger.getName(), logData));
logger.get().log(toLog4jLogEvent(logger, logData));
}

@Override
public void handleError(RuntimeException error, LogData badData) {
logger.get().log(toLog4jLogEvent(logger.getName(), error, badData));
logger.get().log(toLog4jLogEvent(logger, error, badData));
}
}