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

bqucvtff  于 2021-06-07  发布在  Kafka
关注(0)|答案(2)|浏览(423)

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

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)

在Kafka那边,它收到:

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

nafvub8i1#

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

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

我也弄明白了为什么strack trace是为 ConsoleAppender 以及 PatternLayout . 可丢弃的信息由 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();
    }
  }
gg58donl

gg58donl2#

Kafka附录有相当广泛的设置。请参见以下内容:

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不处理loggingevents中包含的throwable。

相关问题