我正在使用 Kafka 生产者客户端,并且我的项目中没有任何 log4j 配置。
运行时,程序会打印大量我真的不想要的 Kafka 调试日志。
因此,我尝试添加 log4j.properties 将日志级别设置为 ERROR,如下所示,这似乎不起作用:
log4j.rootLogger=ERROR
如何更改 Kafka 日志级别?
最佳答案
运行客户端时使用命令行标志-Dlog4j.configuration=file:/path/to/log4j.properties
。
示例 log4j 属性文件:
- https://github.com/apache/kafka/blob/trunk/config/tools-log4j.properties
- 由诸如镜像制作器之类的工具使用,实际上只是标准输出
- https://github.com/apache/kafka/blob/trunk/config/log4j.properties
- 由 kafka 服务器等工具使用
对于镜像制作器和其他导致调用kafka-run-class.sh
的工具,您可以使用环境变量KAFKA_LOG4J_OPTS
(设置为类似-Dlog4j.configuration=file:/path/to/log4j.properties
) 更改日志记录配置。请参阅:https://github.com/apache/kafka/blob/0.10.2/bin/kafka-run-class.sh#L158
我用于测试的镜像制作器的 log4j.properties
文件示例。
# https://github.com/apache/kafka/blob/trunk/config/tools-log4j.properties
log4j.rootLogger=DEBUG, stderr
log4j.appender.stderr=org.apache.log4j.ConsoleAppender
log4j.appender.stderr.layout=org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern=[%d] %p %m (%c)%n
log4j.appender.stderr.Target=System.err
关于logging - 如何为 Kafka 生产者配置日志记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35773780/