Logback Kafka Appender没有将日志写入主题

时间:2018-03-21 19:54:44

标签: java apache-kafka logback

我正在尝试对Logback Kafka Appender进行简单的测试。以下是我在logback.xml中的内容,我将它放在src / main / resources中:

<?xml version="1.0" encoding="UTF-8"?>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> 
    <encoder> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> 
    </encoder> 
</appender>

<!-- This is the kafkaAppender -->
<appender name="kafkaAppender"
    class="com.github.danielwegener.logback.kafka.KafkaAppender">
    <encoder>
        <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
        </pattern>
    </encoder>
    <topic>test</topic>
    <keyingStrategy
        class="com.github.danielwegener.logback.kafka.keying.NoKeyKeyingStrategy" />
    <deliveryStrategy
        class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy" />

    <!-- Optional parameter to use a fixed partition -->
    <!-- <partition>0</partition> -->

    <!-- Optional parameter to include log timestamps into the kafka message -->
    <!-- <appendTimestamp>true</appendTimestamp> -->

    <!-- each <producerConfig> translates to regular kafka-client config (format: 
        key=value) -->
    <!-- producer configs are documented here: https://kafka.apache.org/documentation.html#newproducerconfigs -->
    <!-- bootstrap.servers is the only mandatory producerConfig -->
    <producerConfig>bootstrap.servers=localhost:9092</producerConfig>

    <!-- this is the fallback appender if kafka is not available. -->
    <!-- <appender-ref ref="STDOUT" /> -->
</appender>

<logger name="com.my.package" level="info" additivity="false">
    <appender-ref ref="kafkaAppender" />
</logger>
<root level="info">
    <appender-ref ref="kafkaAppender" />
</root>

我在Windows上启动了一个带有单独Zookeeper的Kafka服务器。我也创建了一个测试主题。

在Windows上启动了Zookeeper,其中包含:zkServer.cmd

在Windows上使用kafka-server-start.bat config\server.properties

启动Kafka Server

创建了一个主题:kafka-topics.bat --create --zookeeper localhost:2181 --replication-factor 1 --partitions 1 --topic test

在Windows上使用kafka-console-consumer.bat --bootstrap-server localhost:9092 --topic test --from-beginning

启动了消费者

在我的Java类中,我所拥有的只是方法中的记录器。

private static final Logger logger = 
LoggerFactory.getLogger(FormController.class);
    logger.debug("DEBUG :: Inside do post method");
    logger.info("INFO :: Inside do post method");

我的进口商品是:

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

我的pom.xml如下:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.my.package</groupId>
<artifactId>SampleIGApp</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SampleIGApp Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.1.0</version>
        <scope>provided</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/log4j/log4j -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-simple</artifactId>
        <version>1.7.21</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.kafka/kafka-clients -->
    <dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka-clients</artifactId>
        <version>1.0.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.github.danielwegener/logback-kafka-appender -->
    <dependency>
        <groupId>com.github.danielwegener</groupId>
        <artifactId>logback-kafka-appender</artifactId>
        <version>0.2.0-RC1</version>
    </dependency>
</dependencies>
<build>
    <finalName>MyApp</finalName>
</build>

有人可以告诉我我做错了什么吗?即使我注释掉STDOUT appender-name,那么日志也会继续写入控制台,在我的例子中是Tomcat的catalina.out。我将不胜感激任何帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

文档说您需要添加logback-classic作为依赖项:

<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.2.3</version>
    <scope>runtime</scope>
</dependency>

logback-kafka-appender