flume入門 log4j 輸出日誌到flume

將log4j產生的日誌直接輸出到flume控制檯

1.編寫客戶端

package com.study.test;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.util.Date;

public class WriteLog {
    protected static final Log logger = LogFactory.getLog(WriteLog.class);


    public static void main(String[] args) throws InterruptedException {

        while (true) {
            // 每隔兩秒log輸出一下當前系統時間戳
            logger.info(new Date().getTime());
            //System.out.println(new Date().getTime());
            Thread.sleep(2000);
            try {
                throw new Exception("exception msg");
            }
            catch (Exception e) {
                logger.error("error:" + e.getMessage());
            }
        }
    }
}

2.pom.xml

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.study.test</groupId>
    <artifactId>log4j2flume</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.apache.flume.flume-ng-clients</groupId>
            <artifactId>flume-ng-log4jappender</artifactId>
            <version>1.7.0</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.1</version>
        </dependency>
    </dependencies>
</project>

3. log4j.properties

log4j.rootLogger=INFO,flume
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern="%d{yyyy-MM-dd HH:mm:ss} %p [%c:%L] - %m%n

log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
log4j.appender.flume.Hostname = 10.45.17.66
log4j.appender.flume.Port = 4444
log4j.appender.flume.UnsafeMode = true
log4j.appender.flume.layout=org.apache.log4j.PatternLayout
log4j.appender.flume.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c:%L] - %m%n

4.配置flume 建立testlog2flume.conf

a1.sources = r1
a1.sinks = k1
a1.channels = c1

# Describe/configure the source
a1.sources.r1.type = avro
a1.sources.r1.bind =0.0.0.0
a1.sources.r1.port = 4444

# Describe the sink
a1.sinks.k1.type=logger
#a1.sinks.k1.type = file_roll
#a1.sinks.k1.sink.directory = /data/soft/flume/tmp
#a1.sinks.k1.sink.rollInterval=86400
#a1.sinks.k1.sink.batchSize=100
#a1.sinks.k1.sink.serializer=text
#a1.sinks.k1.sink.serializer.appendNewline = false

# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 1000

# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

該配置文件中 配置了一個代理a1
在代理agent中配置了一個source(源)一個sink(接收器)和一個channel(通道),分別爲:r1,k1,c1
r1的類型定義爲avro(序列化),對應該類型參數爲bind和port 分別爲0.0.0.0和4141
k1的類型定義爲logger,直接輸出到日誌文件中
cl的類型定義爲內存方式,設置其參數capactiy和transactionCapacity分別爲1000和1000
指定r1和k1的channel爲c1

將上述配置中a1.sinks.k1.type=logger註釋,把a1.sinks其他的給打開,就是將日誌輸入到文件

5.到flume目錄下啓動flume

bin/flume-ng agent -c conf -f conf/testlog2flume.conf --name a1 -Dflume.root.logger=INFO,console

flume-ng :flume 命令
agent:運行一個Flume Agent
-c:在指定目錄下使用配置 use configs in directory
-f:指定配置文件,這個配置文件必須在全局選項的–conf(-c)參數定義的目錄下
–name:Agent的名稱(必填)
-Dflume.root.logger=INFO,console 該參數將會把flume的日誌輸出到console

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章