分享

日志出现“log4j:ERROR Flume append() failed.”,记录解决办法

jixianqiuxue 发表于 2015-3-30 21:53:02 [显示全部楼层] 回帖奖励 阅读模式 关闭右栏 1 47096
日志出现“log4j:ERROR Flume append() failed.”,发现flume-ng-log4jappender-1.4.0有一个bug。详细,请查阅:https://issues.apache.org/jira/browse/FLUME-2186

已有(1)人评论

跳转到指定楼层
jixianqiuxue 发表于 2015-3-30 21:53:50
  解决方法:修改flume-ng-log4jappender中的append方法。修改为:
  1. @Override  
  2. public synchronized void append(LoggingEvent event) throws FlumeException{  
  3.    //If rpcClient is null, it means either this appender object was never  
  4.    //setup by setting hostname and port and then calling activateOptions  
  5.    //or this appender object was closed by calling close(), so we throw an  
  6.    //exception to show the appender is no longer accessible.  
  7.    boolean ready = true;  
  8.    String errorMsg = "Cannot Append to Appender! Appender either closed or" +  
  9.              " not setup correctly!";  
  10.    if (rpcClient == null) {  
  11.      LogLog.error(errorMsg);  
  12.      if (unsafeMode) {  
  13.          activateOptions();  
  14.          if(rpcClient == null || !rpcClient.isActive()) {  
  15.              ready = false;  
  16.          }  
  17.      }else {  
  18.          throw new FlumeException(errorMsg);  
  19.      }  
  20.    }  
  21.    if(unsafeMode && !ready) {  
  22.        return;  
  23.    }  
  24.    if(!rpcClient.isActive()){  
  25.      reconnect();  
  26.    }  
  27.    if(rpcClient == null || !rpcClient.isActive()) {  
  28.        if(unsafeMode) {  
  29.              return;  
  30.          } else {  
  31.              LogLog.error(errorMsg);  
  32.              throw new FlumeException(errorMsg);  
  33.          }  
  34.    }  
  35.    //Client created first time append is called.  
  36.    Map<String, String> hdrs = new HashMap<String, String>();  
  37.    hdrs.put(Log4jAvroHeaders.LOGGER_NAME.toString(), event.getLoggerName());  
  38.    hdrs.put(Log4jAvroHeaders.TIMESTAMP.toString(),  
  39.        String.valueOf(event.getTimeStamp()));  
  40.    //To get the level back simply use  
  41.    //LoggerEvent.toLevel(hdrs.get(Integer.parseInt(  
  42.    //Log4jAvroHeaders.LOG_LEVEL.toString()))  
  43.    hdrs.put(Log4jAvroHeaders.LOG_LEVEL.toString(),  
  44.        String.valueOf(event.getLevel().toInt()));  
  45.    Event flumeEvent;  
  46.    Object message = event.getMessage();  
  47.    if (message instanceof GenericRecord) {  
  48.      GenericRecord record = (GenericRecord) message;  
  49.      populateAvroHeaders(hdrs, record.getSchema(), message);  
  50.      flumeEvent = EventBuilder.withBody(serialize(record, record.getSchema()), hdrs);  
  51.    } else if (message instanceof SpecificRecord || avroReflectionEnabled) {  
  52.      Schema schema = ReflectData.get().getSchema(message.getClass());  
  53.      populateAvroHeaders(hdrs, schema, message);  
  54.      flumeEvent = EventBuilder.withBody(serialize(message, schema), hdrs);  
  55.    } else {  
  56.      hdrs.put(Log4jAvroHeaders.MESSAGE_ENCODING.toString(), "UTF8");  
  57.      String msg = layout != null ? layout.format(event) : message.toString();  
  58.      flumeEvent = EventBuilder.withBody(msg, Charset.forName("UTF8"), hdrs);  
  59.    }  
  60.    try {  
  61.      rpcClient.append(flumeEvent);  
  62.    } catch (EventDeliveryException e) {  
  63.      String msg = "Flume append() failed.";  
  64.      LogLog.error(msg);  
  65.      if (unsafeMode) {  
  66.        return;  
  67.      }  
  68.      throw new FlumeException(msg + " Exception follows.", e);  
  69.    }  
  70. }  
复制代码





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

推荐上一条 /2 下一条