Kafka Log4j.xml appender

时间:2015-11-12 16:17:02

标签: log4j apache-kafka

我已经安装了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 at   kafka.producer.KafkaLog4jAppender.append(KafkaLog4jAppender.scala:72)     在   org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:230)     在   org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:65)     在org.apache.log4j.Category.callAppenders(Category.java:203)at   org.apache.log4j.Category.forcedLog(Category.java:388)at   org.apache.log4j.Category.log(Category.java:853)at   org.slf4j.impl.Log4jLoggerAdapter.log(Log4jLoggerAdapter.java:597)at at   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(StandardHost.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)at   java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)     在   java.util.concurrent.ThreadPoolExecutor中的$ Worker.run(ThreadPoolExecutor.java:615)     在java.lang.Thread.run(Thread.java:745)

1 个答案:

答案 0 :(得分:0)

以下是我的结果。我用kafka 0.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");
}

}