Gab*_*vay 4 java logging log4j kafka-producer-api
我正在使用一个普通的 Java 项目来运行(无框架)Kafka 生产者和消费者。
\n\n我试图控制由KafkaProducer和KafkaConsumer代码生成的日志,但我无法使用log4j.properties配置影响它:
log4j.rootLogger=ERROR,stdout\n\nlog4j.logger.kafka=ERROR,stdout\nlog4j.logger.org.apache.kafka.clients.producer.ProducerConfig=ERROR,stdout\nlog4j.logger.org.apache.kafka.common.utils.AppInfoParser=ERROR,stdout\nlog4j.logger.org.apache.kafka.clients.consumer.internals.AbstractCoordinator=ERROR,stdout\n\nlog4j.appender.stdout=org.apache.log4j.ConsoleAppender\nlog4j.appender.stdout.layout=org.apache.log4j.PatternLayout\nlog4j.appender.stdout.layout.ConversionPattern=[%d] %p %m (%c)%n\nRun Code Online (Sandbox Code Playgroud)\n\n无论我在文件中提供什么设置,我仍然会得到如下所示的日志输出log4j.properties:
[main] INFO org.apache.kafka.clients.producer.ProducerConfig - ProducerConfig values:\n...\n[main] INFO org.apache.kafka.clients.producer.ProducerConfig - ProducerConfig values:\n...\n[main] INFO org.apache.kafka.clients.producer.ProducerConfig - ProducerConfig values:\n...\n[main] INFO org.apache.kafka.clients.consumer.internals.AbstractCoordinator - [Consumer clientId=UM00160, groupId=string-group] (Re-)joining group\nRun Code Online (Sandbox Code Playgroud)\n\n如何控制 Kafka 客户端库的日志记录?log4j.properties将我的文件链接到 Kafka 客户端库日志记录时缺少什么?为了不发送垃圾邮件,我必须使用以下命令运行 Maven 测试mvn test 2> /dev/null:我可以通过log4j.properties.
语境:
\n\n我有以下相关文件:
\n\n\xe2\x94\x80\xe2\x94\x80 test\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 java\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 com\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 example\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 PropertyReader.java\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 strings\n \xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 TestKafkaStringValues.java\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 resources\n \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 application.properties\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 log4j.properties\nRun Code Online (Sandbox Code Playgroud)\n\n我正在尝试TestKafkaStringValues.java使用 Maven Surefire 插件 ( mvn test) 或 Eclipse JUnit 插件(相当于java ...)来运行这两个插件。
为了确保万无一失,我在 Maven 中使用以下配置pom.xml:
<plugin>\n <artifactId>maven-surefire-plugin</artifactId>\n <version>2.22.2</version>\n <configuration>\n <systemPropertyVariables>\n <log4j.configuration>file:log4j.properties</log4j.configuration>\n </systemPropertyVariables>\n </configuration>\n</plugin>\nRun Code Online (Sandbox Code Playgroud)\n\n对于 JUnit,我使用以下 Java VM 参数:-Dlog4j.configuration=log4j.properties。
我还尝试在这两种情况下使用 的绝对路径log4j.properties。还是行不通。
您可以在此处查看完整的代码。
\n上面代码中的问题是 Maven 运行时依赖项(缺少实际的 Log4j 日志记录实现)。在pom中,slf4j-simple提供了日志记录的实现。这个实现是:
log4j.properties或-Dlog4j.*属性。因此,once 必须包含在 Log4J 实现中。这里可以选择Log4j 1.x(生命周期结束)或Log4j2。
通过以下配置,人们应该能够对日志记录(包括 Kafka 客户端)进行非常全面/精细的控制。
在里面pom.xml:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.13.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.13.1</version>
<scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
而log4j-api和log4j-core是您需要的最低要求。为了让 Log4j2 也能够控制/配置在 SLF4J 之上编写的库/组件(Kafka 客户端就是这样一个库),您需要添加第三个依赖项:log4j-slf4j-impl。
注意:请注意,对于使用 SLF4J 1.8.x 及更高版本的库,您将需要此 Log4j-SLF4J 适配器的另一个版本。请参阅此了解更多信息。
现在,关于配置日志记录,Log4j2会自动加载它找到的配置文件,并自动在多个位置进行搜索。
如果将以下log4j2.properties文件放置在资源类路径中(用于src/java/resources/主代码和用于src/test/resource测试代码),您将获得所需的结果:
rootLogger.level = info
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
appenders = stdout
appender.stdout.name = STDOUT
appender.stdout.type = Console
appender.stdout.layout.type = PatternLayout
appender.stdout.layout.pattern =%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%t] %c - %m%n
loggers = kafka, kafka-consumer
logger.kafka.name = org.apache.kafka
logger.kafka.level = warn
logger.kafka-consumer.name = org.apache.kafka.clients.consumer
logger.kafka-consumer.level = info
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,所有日志记录均写入stdout以下内容: * 根记录器正在记录info以上内容 * 所有org.apache.kafka前缀记录器warn正在记录以上内容 * 所有org.apache.kafka.clients.consumer前缀记录器正在记录info以上内容
以下是使用 Log4j2 时的一些额外观察结果:
mvn但输出会显示错误。| 归档时间: |
|
| 查看次数: |
18620 次 |
| 最近记录: |