我想使用log4j kafka附加程序将我的应用日志消息流式传输到kafka。
我的应用程序正在使用spring boot和log4j2。
我在主题中看到启动日志消息,但在应用程序中看不到日志语句。
我在log4j2配置中没有错过任何事情吗?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="kafka-logs"
packages="myapp.kafka.test">
<Appenders>
<Kafka name="Kafka" topic="test-kafka-topic">
<JSONLayout />
<Property name="bootstrap.servers">server1,server2</Property>
</Kafka>
<Async name="Async">
<AppenderRef ref="Kafka"/>
</Async>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5p [%-7t] %F:%L - %m%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Kafka"/>
<AppenderRef ref="stdout"/>
</Root>
<Logger name="org.apache.kafka" level="WARN" />
</Loggers>
pom.xml
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.10</version>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-log4j-appender</artifactId>
<version>1.0.0</version>
<exclusions>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
Java代码
@SpringBootApplication
public class TestApplication {
private static final Logger logger =
LoggerFactory.getLogger(TestApplication.class);
public static void main(String[] args) {
SpringApplication application = new SpringApplication(TestApplication.class);
logger.info("Before Application Startup");
application.run(args);
logger.info("After Application Startup");
}
}
登录Kafka:
{
"thread" : "main",
"level" : "INFO",
"loggerName" : "TestApplication",
"message" : "Started TestApplication in 17.19 seconds (JVM running for 22.67)",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.commons.logging.LogFactory$Log4jLog",
"instant" : {
"epochSecond" : 1555422668,
"nanoOfSecond" : 856000000
},
"threadId" : 1,
"threadPriority" : 5
}
控制台输出:
08:51:08.856 [main] INFO TestApplication - Started TestApplication in 17.19 seconds (JVM running for 22.67)
[main] INFO TestApplication - Testing my application from TestApplication
[main] ERROR TestApplication - Testing my application from TestApplication
答案 0 :(得分:0)
我认为log4j2已经具有Kafka附加程序。对于我来说,我在gradle中添加了Kafka客户程序。因此,无需添加kafka-log4j-appender
compile group: 'org.apache.kafka', name: 'kafka-clients', version: '2.0.1'
Maven应该相似
答案 1 :(得分:0)
您需要按以下方式排除spring-boot-starter-logging
/* latin */
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 300;
src: local('Roboto Light'), local('Roboto-Light'), url(https://fonts.gstatic.com/s/roboto/v18/KFOlCnqEu92Fr1MmSU5fBBc4.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
font-display: swap;
}
答案 2 :(得分:0)
首先,您需要在 pom.xml 文件中删除 spring boot 应用程序中日志的默认提供程序,即 Logback 和 Log-classic,然后您需要添加 log4j2 作为新的日志提供程序并添加 Kafka appender。 添加依赖项后,您需要 xml 配置文件,您可以在其中添加 Kafka appender 配置。 默认情况下,您需要在项目的资源路径中找到您的配置文件,并将其命名为“log4j2.xml”。
您可以找到许多其他 Log4j2 appenders,例如 Cassandra 或故障转移附加程序,并将它们添加到配置文件中的 Kafka 附加程序旁边。您可以在下面找到适用且正确的示例。
<!--excluding logback -->
<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>
<!--adding log4j2 and kafka appender-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-log4j-appender</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
</exclusions>
</dependency>
Kafka appender 配置
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="info" name="kafka-appender" packages="Citydi.ElasticDemo">
<Appenders>
<Kafka name="kafkaLogAppender" topic="Second-Topic">
<JSONLayout />
<Property name="bootstrap.servers">localhost:9092</Property>
<MarkerFilter marker="Recorder" onMatch="DENY" onMismatch="ACCEPT"/>
</Kafka>
<Console name="stdout" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} stdout %highlight{%-5p} [%-7t] %F:%L - %m%n"/>
<MarkerFilter marker="Recorder" onMatch="DENY" onMismatch="ACCEPT"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="kafkaLogAppender"/>
<AppenderRef ref="stdout"/>
</Root>
<Logger name="org.apache.kafka" level="warn" />
</Loggers>
</Configuration>