gpt4 book ai didi

logging - Spring-Boot 日志记录到 Kafka : how to eliminate warning; best practices

转载 作者:行者123 更新时间:2023-12-04 18:59:11 24 4
gpt4 key购买 nike

我被问到如何从作为 Oozie 的 Java 操作运行的 Spring Boot 应用程序捕获日志记录输出。

我最初的想法是可以编辑一些 log4j 属性来捕获 YARN 或 Oozie 中的应用程序日志。然后我突然想到,对于在各种集群节点上运行的特定应用程序,Kafka 将是一种更容易捕获和聚合日志消息的方法。通过订阅主题来监视分布式系统比通过日志文件钓鱼要容易得多。

我注意到 Kafka 有一个 log4j appender,所以我尝试创建一个最小的可重现示例(发布在 github 上:https://github.com/alexwoolford/spring-boot-log-to-kafka-example)。这是来自 pom.xml 的片段:

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.4.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-log4j-appender</artifactId>
<version>0.10.0.0</version>
</dependency>
<dependency>
<groupId>net.logstash.log4j</groupId>
<artifactId>jsonevent-layout</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
</dependencies>

我的 log4j.properties 文件如下所示:
log4j.rootLogger=INFO
log4j.appender.KAFKA=org.apache.kafka.log4jappender.KafkaLog4jAppender
log4j.appender.KAFKA.layout=net.logstash.log4j.JSONEventLayoutV1
log4j.appender.KAFKA.topic=logs
log4j.appender.KAFKA.brokerList=hdp-single-node:6667
log4j.appender.KAFKA.syncSend=true
log4j.appender.KAFKA.producer.type=async
log4j.logger.io.woolford=INFO, KAFKA

这有效,只是它会生成警告:
log4j:WARN No appenders could be found for logger (org.apache.kafka.clients.producer.ProducerConfig).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

尽管此应用程序运行并执行我需要的操作,但警告表明我配置错误。你能看出需要改变什么吗?

另外,我注意到 Spring Boot 默认使用 Logback我注意到有一个开源项目, logback-kafka-appender ,这允许 Logback 附加到 Kafka。 Kafka log4j appender 是 Spring Boot 登录到 Kafka 的最佳方式吗?

最佳答案

Log4j2 有一个 Kafka appender .有必要添加 spring-boot-starter-log4j2jackson-databind文物到 pom.xml :

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-log4j-appender</artifactId>
<version>0.10.0.0</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>

然后我创建了一个 XML 格式 log4j2.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" name="spring-boot-log-to-kafka-example" packages="io.woolford">
<Appenders>
<Kafka name="kafkaAppender" topic="logs">
<JSONLayout />
<Property name="bootstrap.servers">hdp-single-node:6667</Property>
</Kafka>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="kafkaAppender"/>
</Root>
<Logger name="org.apache.kafka" level="WARN" />
</Loggers>
</Configuration>

日志消息以 JSON 格式发送到 Kafka,例如
{
"timeMillis": 1485736022854,
"thread": "Thread-1",
"level": "INFO",
"loggerName": "org.springframework.context.annotation.AnnotationConfigApplicationContext",
"message": "Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@20140db9: startup date [Sun Jan 29 17:26:52 MST 2017]; root of context hierarchy",
"endOfBatch": false,
"loggerFqcn": "org.apache.commons.logging.impl.SLF4JLocationAwareLog",
"threadId": 19,
"threadPriority": 5
}

关于logging - Spring-Boot 日志记录到 Kafka : how to eliminate warning; best practices,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41925737/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com