loh4j整合flume(log4jsource)

原创
2016/01/17 11:21
阅读数 381

一、配置启动flume

    ①flume的配置文件  conf/flume-conf.properties(这个文件一般是flume-conf.properties.default),复制修改后缀即可;

    配置文件如下:

    agent1.channels = ch1
    agent1.sources = avro-source1
    agent1.sinks = log-sink1


    # 定义channel
    agent1.channels.ch1.type = memory    #mem channel


    # 定义source
    agent1.sources.avro-source1.channels = ch1
    agent1.sources.avro-source1.type = avro        #avro source
    agent1.sources.avro-source1.bind = 0.0.0.0
    agent1.sources.avro-source1.port = 41414


    # 定义sink
    agent1.sinks.log-sink1.channel = ch1
    agent1.sinks.log-sink1.type = logger                #logger sink

    启动flume

    bin/flume-ng agent --conf conf --conf-file conf/flume-conf.properties --name agent1 -Dflume.root.logger=INFO,console

二、log4j的配置

    ①在log4j.properties文件中添加flume appender

    log4j.rootLogger=INFO,flume   #日志级别,info和flume

    log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
    log4j.appender.flume.Hostname = 192.168.57.133    #flume地址
    log4j.appender.flume.Port = 41414                          #flume source监听的端口
    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,SSS} [%t] [%c] [%p] - %m%n

三、测试

    在maven中添加flume appender依赖

<dependency>
<groupId>org.apache.flume.flume-ng-clients</groupId>
<artifactId>flume-ng-log4jappender</artifactId>
<version>1.6.0</version>
</dependency>



    编写简单的测试类:

    

public class Log4jTest {
	Logger logger = LoggerFactory.getLogger(Log4jTest.class);	/**
	 * 测试flume
	* @Title: flumeTest 
	* @Description: TODO
	* @param @throws Exception    
	* @return void    
	* @throws
	 */
	@Test
	public void flumeTest() throws Exception {
		while(true){
			logger.info("spider", System.currentTimeMillis());
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}



    运行测试类,可以看到flume在控制台的输出

    

展开阅读全文
打赏
1
1 收藏
分享
加载中
更多评论
打赏
0 评论
1 收藏
1
分享
返回顶部
顶部