log4j:ERROR Flume append() failed.(flume-ng-log4jappender-1.4.0.jar bug)

       日志出现“log4j:ERROR Flume append() failed.”,发现flume-ng-log4jappender-1.4.0有一个bug。详细,请查阅:https://issues.apache.org/jira/browse/FLUME-2186。

       解决方法:修改flume-ng-log4jappender中的append方法。修改为:

 @Override
  public synchronized void append(LoggingEvent event) throws FlumeException{
    //If rpcClient is null, it means either this appender object was never
    //setup by setting hostname and port and then calling activateOptions
    //or this appender object was closed by calling close(), so we throw an
    //exception to show the appender is no longer accessible.
    boolean ready = true;
    String errorMsg = "Cannot Append to Appender! Appender either closed or" +
              " not setup correctly!";
    if (rpcClient == null) {
      LogLog.error(errorMsg);
      if (unsafeMode) {
          activateOptions();
          if(rpcClient == null || !rpcClient.isActive()) {
              ready = false;
          }
      }else {
          throw new FlumeException(errorMsg);
      }
    }
    if(unsafeMode && !ready) {
        return;
    }

    if(!rpcClient.isActive()){
      reconnect();
    }
    if(rpcClient == null || !rpcClient.isActive()) {
        if(unsafeMode) {
              return;
          } else {
              LogLog.error(errorMsg);
              throw new FlumeException(errorMsg);
          }
    }
    //Client created first time append is called.
    Map<String, String> hdrs = new HashMap<String, String>();
    hdrs.put(Log4jAvroHeaders.LOGGER_NAME.toString(), event.getLoggerName());
    hdrs.put(Log4jAvroHeaders.TIMESTAMP.toString(),
        String.valueOf(event.getTimeStamp()));

    //To get the level back simply use
    //LoggerEvent.toLevel(hdrs.get(Integer.parseInt(
    //Log4jAvroHeaders.LOG_LEVEL.toString()))
    hdrs.put(Log4jAvroHeaders.LOG_LEVEL.toString(),
        String.valueOf(event.getLevel().toInt()));

    Event flumeEvent;
    Object message = event.getMessage();
    if (message instanceof GenericRecord) {
      GenericRecord record = (GenericRecord) message;
      populateAvroHeaders(hdrs, record.getSchema(), message);
      flumeEvent = EventBuilder.withBody(serialize(record, record.getSchema()), hdrs);
    } else if (message instanceof SpecificRecord || avroReflectionEnabled) {
      Schema schema = ReflectData.get().getSchema(message.getClass());
      populateAvroHeaders(hdrs, schema, message);
      flumeEvent = EventBuilder.withBody(serialize(message, schema), hdrs);
    } else {
      hdrs.put(Log4jAvroHeaders.MESSAGE_ENCODING.toString(), "UTF8");
      String msg = layout != null ? layout.format(event) : message.toString();
      flumeEvent = EventBuilder.withBody(msg, Charset.forName("UTF8"), hdrs);
    }

    try {
      rpcClient.append(flumeEvent);
    } catch (EventDeliveryException e) {
      String msg = "Flume append() failed.";
      LogLog.error(msg);
      if (unsafeMode) {
        return;
      }
      throw new FlumeException(msg + " Exception follows.", e);
    }
  }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值