Flume(ng) 自訂sink實現和屬性注入

來源:互聯網
上載者:User

轉自:http://www.aboutyun.com/thread-7884-1-1.html

問題導讀:
1.如何?flume端自定一個sink,來按照我們的規則來儲存日誌。
2.想從flume的設定檔中擷取rootPath的值,該如何配置。






最近需要利用flume來做收集遠端日誌,所以學習一些flume最基本的用法。這裡僅作記錄。

遠端日誌收集的整體思路是遠端自訂實現log4j的appender把訊息發送到flume端,flume端自訂實現一個sink來按照我們的規則儲存日誌。

自訂Sink代碼:

public class LocalFileLogSink extends AbstractSink implements Configurable {
     private static final Logger logger = LoggerFactory
              . getLogger(LocalFileLogSink .class );
            private static final String PROP_KEY_ROOTPATH = "rootPath";
      private String rootPath;
     @Override
     public void configure(Context context) {
          String rootPath = context.getString(PROP_KEY_ROOTPATH );
          setRootPath(rootPath);
     }
           
          @Override
          public Status process() throws EventDeliveryException {
           logger .debug("Do process" );
       }
} 複製代碼 實現Configurable介面,即可在初始化時,通過configure方法從context中擷取配置的參數的值。這裡,我們是想從flume的設定檔中擷取rootPath的值,也就是日誌儲存的根路徑。在flume-conf.properties中配置如下:
agent.sinks = loggerSink
agent.sinks.loggerSink.rootPath = ./logs 複製代碼
loggerSink是自訂sink的名稱,我們取值時的key,只需要loggerSink後面的部分即可,即這裡的rootPath。

實際商務邏輯的執行,是通過繼承複寫AbstractSink中的process方法實現。從基類的getChannel方法中擷取通道,從中取出Event處理即可。

Channel ch = getChannel();
           Transaction txn = ch.getTransaction();
         txn.begin();
          try {
              logger .debug("Get event." );
             Event event = ch.take();
             txn.commit();
             status = Status. READY ;
             return status;
                   }finally {
             Log. info( "trx close.");
             txn.close();
         } 複製代碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.