javaapache-kafkalog4j2

Library using sfl4j not following log42.xml configurations


I'm using the kafka-clients library in my Java application, and the logs are just not getting appended to the Kafka log I specified in my log4j2.xml. However, the org.springframework.kafka logs are getting appended to the log I specified just fine.

I noticed that kafka-clients uses slf4j, is the root cause related to that?

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration xmlns="https://logging.apache.org/xml/ns"
               xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:schemaLocation="
                   https://logging.apache.org/xml/ns
                   https://logging.apache.org/xml/ns/log4j-config-2.xsd">
  <Appenders>
    <Console name="CONSOLE">
      <PatternLayout pattern="%p - %m%n"/>
    </Console>
    <File name="MAIN" fileName="logs/main.log">
      <JsonTemplateLayout/>
    </File>
    <File name="KAFKALOG" fileName="logs/kafka.log">
      <PatternLayout pattern="%d [%t] %p %c - %m%n"/>
    </File>
  </Appenders>
  <Loggers>
    <Root level="INFO">
      <AppenderRef ref="CONSOLE" level="WARN"/>
      <AppenderRef ref="MAIN"/>
    </Root>
        <Logger name="org.springframework.kafka" level="INFO">
            <AppenderRef ref="KAFKALOG"/>
        </Logger>

        <Logger name="org.apache.kafka" level="INFO">
            <AppenderRef ref="KAFKALOG"/>
        </Logger>
  </Loggers>
</Configuration>

Solution

  • You need to add the SLF4J to Log4j2 bridge dependency to your project.

     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
                <version>${org.springframework.boot.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-logging</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
    
     <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-log4j2</artifactId>
                <version>${org.springframework.boot.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-jul</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.apache.logging.log4j</groupId>
                        <artifactId>log4j-slf4j-impl</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>org.slf4j</groupId>
                        <artifactId>jul-to-slf4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>org.apache.logging.log4j</groupId>
                <artifactId>log4j-spring-boot</artifactId>
                <version>2.20.0</version>
            </dependency>
    

    and you might want to modify the Lafka logger configuration slightly to capture all Kafka-related logs

    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="CONSOLE" level="WARN"/>
            <AppenderRef ref="MAIN"/>
        </Root>
        
        <!-- Combine both Kafka loggers into one -->
        <Logger name="kafka" level="INFO" additivity="false">
            <AppenderRef ref="KAFKALOG"/>
        </Logger>
        
        <Logger name="org.apache.kafka" level="INFO" additivity="false">
            <AppenderRef ref="KAFKALOG"/>
        </Logger>
        
        <Logger name="org.springframework.kafka" level="INFO" additivity="false">
            <AppenderRef ref="KAFKALOG"/>
        </Logger>
    </Loggers>