kafka client print log

kafka 0.10.0 java client uses slf4j as the log facade, we need to add specific log implementation dependencies to print logs, log framework: http://www.cnblogs.com/set-cookie/p/8836496.html

1 The client depends on the jar package

use command

mvn dependency:tree -Dverbose

Looking at the packages that the client depends on, you can see that the java client only depends on slf4j, and there is no specific log implementation:
kafka dependency

Running the client will print the warning prompt of slf4j
sl4j warn

2 Using log4j2

add dependencies

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>3.8.1</version>
            <scope>test</scope>
        </dependency>

        <!-- kafka客户端-->
        <dependency>
            <groupId>org.apache.kafka</groupId>
            <artifactId>kafka-clients</artifactId>
            <version>0.10.0.0</version>
        </dependency>

        <!--log4j2到slf4j桥梁-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-slf4j-impl</artifactId>
            <version>2.9.1</version>
        </dependency>

        <!--log4j2-->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
            <version>2.11.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.11.0</version>
        </dependency>
        <!--log4j2-->
    </dependencies>

If the log4j2.xml configuration file is not created, log4j2 will use the default configuration and the output level is ERROR, as follows:

 <?xml version="1.0" encoding="UTF-8"?>
  <Configuration status="WARN"> <!--status表示log4j2自身日志的级别-->
    <Appenders>
      <Console name="Console" target="SYSTEM_OUT">
        <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
      </Console>
    </Appenders>
    <Loggers>
      <Root level="error">
             <AppenderRef ref="Console"/>
                  </Root>
   </Loggers>
 </Configuration>

For flexibility, we can create log4j2.xml in the resources directory to customize the printing of log4j2, in fact, just change the log level from error to trace

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="trace"> <!--error改成trace,便于debug-->
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

Run it again, the trace log has been printed
kafka trace

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324386089&siteId=291194637