kafka-log4j-appender 0.9.0.1省略了错误的可抛出信息

时间:2016-03-30 14:39:04

标签: java log4j apache-kafka appender

看起来kafka-log4j-appender 0.9.0.1在向Kafka发送消息时省略了throwable信息。 我正在使用以下配置:

log4j.rootLogger=INFO, stdout
log4j.logger.kafka=INFO, stdout
log4j.logger.com.doublev=INFO, KAFKA

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.KAFKA=org.apache.kafka.log4jappender.KafkaLog4jAppender
log4j.appender.KAFKA.layout=org.apache.log4j.PatternLayout
log4j.appender.KAFKA.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
log4j.appender.KAFKA.brokerList=localhost:9092
log4j.appender.KAFKA.topic=logs
log4j.appender.KAFKA.syncSend=false

控制台中的示例输出:

ERROR [main] (App.java:21) - / by zero
java.lang.ArithmeticException: / by zero
    at com.doublev.sls.test.App.main(App.java:19)

在卡夫卡方面,它收到了:

ERROR [main] (App.java:21) - / by zero

2 个答案:

答案 0 :(得分:1)

Kafka appender拥有相当广泛的设置。请参阅以下内容:

log4j.appender.KAFKA.compressionType=none
log4j.appender.KAFKA.requiredNumAcks=0
log4j.appender.KAFKA.syncSend=true  //did you try false here?
log4j.appender.KAFKA.Serializer=kafka.producer.DefaultStringEncoder

也请使用

log4j.appender.KAFKA.layout=org.apache.log4j.EnhancedPatternLayout

根据 log4j java doc

  

PatternLayout 无法处理其中包含的throwable   LoggingEvents。

答案 1 :(得分:1)

对我有用的最终配置如下:

log4j.appender.KAFKA=org.apache.kafka.log4jappender.KafkaLog4jAppender
log4j.appender.KAFKA.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.KAFKA.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n%throwable
log4j.appender.KAFKA.brokerList=localhost:9092
log4j.appender.KAFKA.topic=logs
log4j.appender.KAFKA.syncSend=false

我还想出了为ConsoleAppenderPatternLayout打印strack跟踪的原因。可抛出的信息由ConsoleAppender前任 - WriterAppender处理:

protected
  void subAppend(LoggingEvent event) {
    this.qw.write(this.layout.format(event));

    if(layout.ignoresThrowable()) {
      String[] s = event.getThrowableStrRep();
      if (s != null) {
    int len = s.length;
    for(int i = 0; i < len; i++) {
      this.qw.write(s[i]);
      this.qw.write(Layout.LINE_SEP);
    }
      }
    }

    if(shouldFlush(event)) {
      this.qw.flush();
    }
  }