I have this config:
pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>1.0.1</version>
</dependency>
resources/log4j2.properties
name=PropertiesConfig
appenders = console
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
rootLogger.level = ERROR
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
When I start my Kafka consumer using org.apache.kafka.clients.consumer.KafkaConsumer
, it's logging at the INFO
level. Any new logger I create using LogManager.getLogger(name)
honors the configuration and logs at level ERROR
, so I know the log4j2.properties
file is being loaded and used. Kafka seems to be ignoring it though. In the logs, when the Kafka consumer is starting up, I see this line a few time:
Aug 20, 2018 11:03:37 PM org.apache.kafka.common.utils.LogContext$KafkaLogger info
Any help appreciated!
Kafka logs via SLF4J. If you want to use Log4j2, you have to configure the two to work together (by adding the SLF4J implementation provided by Log4j2):
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.1</version>
</dependency>
Without this, log output from SLF4J will use some other backend (make sure you remove that one, you should not have any other slf4j-impl on your classpath) and ignore your Log4j2 configuration.