Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

KAFKA-18046: cache calls to LoggerFactory.getLogger() inside LogContext #17896

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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 @@ -23,6 +23,8 @@
import org.slf4j.helpers.MessageFormatter;
import org.slf4j.spi.LocationAwareLogger;

import java.util.concurrent.ConcurrentHashMap;

/**
* This class provides a way to instrument loggers with a common context which can be used to
* automatically enrich log messages. For example, in the KafkaConsumer, it is often useful to know
Expand All @@ -32,6 +34,7 @@
*/
public class LogContext {

private static final ConcurrentHashMap<Class<?>, Logger> loggerCache = new ConcurrentHashMap<>();

Check notice on line 37 in clients/src/main/java/org/apache/kafka/common/utils/LogContext.java

View workflow job for this annotation

GitHub Actions / build / Compile and Check Java

Checkstyle error

Name 'loggerCache' must match pattern '(^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$)|(^log$)'.
private final String logPrefix;

public LogContext(String logPrefix) {
Expand All @@ -43,7 +46,7 @@
}

public Logger logger(Class<?> clazz) {
Logger logger = LoggerFactory.getLogger(clazz);
Logger logger = loggerCache.computeIfAbsent(clazz, LoggerFactory::getLogger);
if (logger instanceof LocationAwareLogger) {
return new LocationAwareKafkaLogger(logPrefix, (LocationAwareLogger) logger);
} else {
Expand Down
Loading