为Log4j Appender运行Kafka-0.8.1时出错

时间:2014-04-18 00:23:16

标签: java scala log4j apache-kafka

我正在尝试使用log4j appender将日志数据推送到kafka代理。我无法将消息推送到经纪人,是否有人可以帮我解决这个问题?我使用的是log4j-1.2.15jar和kafka_2.9.2-0.8.1.jar。这是我的追随者:

<?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

    <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

        <appender name="console" class="org.apache.log4j.ConsoleAppender">
            <param name="Target" value="System.out"/>
            <layout class="org.apache.log4j.PatternLayout">
                <param name="ConversionPattern" value="%-5p %c{1} - %m%n"/>
            </layout>
        </appender>


     <appender class="kafka.producer.KafkaLog4jAppender" name="kafka">
        <param name="BrokerList" value="oos007:9092"/>
        <param name="Topic" value="SessionSpy"/>
        <layout class="org.apache.log4j.PatternLayout">
          <param value="%d{ISO8601} %p %c{2} - %m%n" name="ConversionPattern"/>
        </layout>
      </appender>


      <logger name="kafka.push">
        <level value="ALL"/>
        <appender-ref ref="kafka"/>
      </logger>

        <root>
            <level value="ALL"/>
            <appender-ref ref="console"/>
        </root>


    </log4j:configuration>

我的测试课程是:

/**
 * 
 */
package com.kafka.test;

import org.apache.log4j.Logger;

public class KafkaLogTester {

    protected static final Logger logger = Logger.getLogger(KafkaLogTester.class);

    public static void main(String[] args) {

        logger.info("OMG! It works! ");
    }

}

我收到了这些错误。

log4j:WARN No appenders could be found for logger (kafka.utils.VerifiableProperties).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN Error during default initialization
java.lang.NoClassDefFoundError: com/yammer/metrics/Metrics
    at kafka.metrics.KafkaMetricsGroup$class.newMeter(KafkaMetricsGroup.scala:46)
    at kafka.producer.ProducerStats.newMeter(ProducerStats.scala:23)
    at kafka.producer.ProducerStats.<init>(ProducerStats.scala:24)
    at kafka.producer.ProducerStatsRegistry$$anonfun$1.apply(ProducerStats.scala:33)
    at kafka.producer.ProducerStatsRegistry$$anonfun$1.apply(ProducerStats.scala:33)
    at kafka.utils.Pool.getAndMaybePut(Pool.scala:61)
    at kafka.producer.ProducerStatsRegistry$.getProducerStats(ProducerStats.scala:37)
    at kafka.producer.async.DefaultEventHandler.<init>(DefaultEventHandler.scala:48)
    at kafka.producer.Producer.<init>(Producer.scala:59)
    at kafka.producer.KafkaLog4jAppender.activateOptions(KafkaLog4jAppender.scala:84)
    at org.apache.log4j.config.PropertySetter.activate(PropertySetter.java:257)
    at org.apache.log4j.xml.DOMConfigurator.parseAppender(DOMConfigurator.java:285)
    at org.apache.log4j.xml.DOMConfigurator.findAppenderByName(DOMConfigurator.java:171)
    at org.apache.log4j.xml.DOMConfigurator.findAppenderByReference(DOMConfigurator.java:184)
    at org.apache.log4j.xml.DOMConfigurator.parseChildrenOfLoggerElement(DOMConfigurator.java:502)
    at org.apache.log4j.xml.DOMConfigurator.parseCategory(DOMConfigurator.java:415)
    at org.apache.log4j.xml.DOMConfigurator.parse(DOMConfigurator.java:919)
    at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:790)
    at org.apache.log4j.xml.DOMConfigurator.doConfigure(DOMConfigurator.java:696)
    at org.apache.log4j.helpers.OptionConverter.selectAndConfigure(OptionConverter.java:471)
    at org.apache.log4j.LogManager.<clinit>(LogManager.java:125)
    at org.apache.log4j.Logger.getLogger(Logger.java:118)
    at com.kafka.test.KafkaLogTester.<clinit>(KafkaLogTester.java:16)
Caused by: java.lang.ClassNotFoundException: com.yammer.metrics.Metrics
    at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 23 more

1 个答案:

答案 0 :(得分:0)

确保添加了相同版本的scala库。当我使用比Kafka的Scala版本更旧的scala lib时,我遇到了同样的问题。