kafka 0.10.0 java客户端使用slf4j作为日志门面,需要我们加入具体的日志实现依赖才能打印日志,日志框架:http://www.cnblogs.com/set-cookie/p/8836496.html
1 客户端依赖jar包
使用命令
mvn dependency:tree -Dverbose
查看客户端依赖的那些包,可以看到java客户端只依赖了slf4j,并没有具体的日志实现:
运行客户端,会打印slf4j的warn提示
2 使用log4j2
加入依赖
<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 begin-->
<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 end-->
如果不创建log4j2.xml配置文件的话,log4j2会使用默认的配置,输出级别是ERROR,如下:
<?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>
为了灵活性,我们可以在resources目录下创建log4j2.xml来自定义log4j2的打印,其实仅仅是将日志级别从error修改为了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>
再次运行, 已经打印了trace日志