kafka log4j.xml附加器

syqv5f0l  于 2021-06-08  发布在  Kafka
关注(0)|答案(1)|浏览(502)

我已经安装了kafka并且工作正常,现在我正在尝试将log appender添加到现有应用程序的log4j.xml文件中,这样我就可以将消息推送到kafka中,所以log4j.xml kafka appender的任何示例都会非常有帮助,我在pom.xml中添加了以下依赖项

<dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.10</artifactId>
        <version>0.8.2.2</version>
    </dependency>

下面是我添加到log4j.xml中的内容

<appender name="kafka" class="kafka.producer.KafkaLog4jAppender">   
        <param name="topic" value="test" />
        <param name="brokerList" value="localhost:9092" />
        <param name="compressionType" value="none" />
        <param name="requiredNumAcks" value="0" />
        <param name="syncSend" value="true" />  

        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[%d{dd.MM.yyyy HH:mm:ss}] %p: %c{1}.%M() - %m%n" />
        </layout>
    </appender>

这是添加这个之后的错误
java.lang.nosuchmethoderror:org.apache.log4j.spi.loggingevent.gettimestamp()j位于kafka.producer.kafkalog4jappender.append(kafkalog4jappender。scala:72)在org.apache.log4j.appenderskeleton.doappend(appenderskeleton。java:230)在org.apache.log4j.helpers.appenderatachableimpl.appenderlooponappenders(appenderatachableimpl。java:65)在org.apache.log4j.category.callappenders(category。java:203)在org.apache.log4j.category.forcedlog(category。java:388)在org.apache.log4j.category.log(category。java:853)在org.slf4j.impl.log4jloggeradapter.log(log4jloggeradapter。java:597)在org.apache.commons.logging.impl.slf4jlocationawarelog.info(slf4jlocationawarelog。java:159)在org.springframework.web.context.contextloader.initwebapplicationcontext(contextloader。java:272)在org.springframework.web.context.contextloaderlistener.contextinitialized(contextloaderlistener。java:111)在org.apache.catalina.core.standardcontext.listenerstart(standardcontext。java:4939)在org.apache.catalina.core.standardcontext.startinternal(standardcontext。java:5434)在org.apache.catalina.util.lifecyclebase.start(lifecyclebase。java:150)在org.apache.catalina.core.containerbase.addchildinternal(containerbase。java:901)在org.apache.catalina.core.containerbase.addchild(containerbase。java:877)在org.apache.catalina.core.standardhost.addchild(标准主机。java:633)在org.apache.catalina.startup.hostconfig.deploydescriptor(hostconfig。java:656)在org.apache.catalina.startup.hostconfig$deploydescriptor.run(hostconfig。java:1635)在java.util.concurrent.executors$runnableadapter.call(executors。java:471)在java.util.concurrent.futuretask.run(futuretask。java:262)位于java.util.concurrent.threadpoolexecutor.runworker(threadpoolexecutor。java:1145)在java.util.concurrent.threadpoolexecutor$worker.run(threadpoolexecutor。java:615)在java.lang.thread.run(线程。java:745)

63lcw9qa

63lcw9qa1#

这是我的结果。我用Kafka0.8.2.2测试过。请确保添加以下依赖项

<dependency>
        <groupId>org.apache.kafka</groupId>
        <artifactId>kafka_2.10</artifactId>
        <version>0.8.2.2</version>
    </dependency>

下面是示例log4j文件:

log4j.rootLogger=INFO
 log4j.appender.KAFKA=kafka.producer.KafkaLog4jAppender
 log4j.appender.KAFKA.BrokerList=sandbox.hortonworks.com:6667
 log4j.appender.KAFKA.Topic=test2
 log4j.appender.KAFKA.layout=org.apache.log4j.PatternLayout    
 log4j.appender.KAFKA.layout.ConversionPattern=%m%n
 log4j.appender.KAFKA.syncsend=true
 log4j.logger.org.nk.kafkatut.producer=INFO, KAFKA

下面是我使用log4j的示例应用程序代码:-

package org.nk.kafkatut.producer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Log4jAppenderDemo {
public static final Logger LOG =   LoggerFactory.getLogger(Log4jAppenderDemo.class.getName());
public static void main(String[] args) {
    LOG.info("test message:1");
    LOG.info("test message:2");
}

}

相关问题