java Kafka:如何启用客户端日志记录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35044735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 23:40:35  来源:igfitidea点击:

Kafka: How do I enable client logging?

javaloggingapache-kafkaslf4j

提问by Mark Harrison

When I instantiate a Kafka consumer

当我实例化一个 Kafka 消费者时

KafkaConsumer<String,String> consumer = new KafkaConsumer<String,String>(props);

I get this message

我收到这条消息

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

How do I enable logging for my client program?

如何为我的客户端程序启用日志记录?

回答by Mark Harrison

Add this property file src/main/resources/log4j.propertiesto your project:

将此属性文件添加src/main/resources/log4j.properties到您的项目中:

$ cat src/main/resources/log4j.properties 
# Root logger option
log4j.rootLogger=DEBUG, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

This will enable logging. You can then set the standard options such as debug level, output format, etc, as per the logging documentation.

这将启用日志记录。然后,您可以根据日志记录文档设置标准选项,例如调试级别、输出格式等。