flume学习(一):log4j直接输出日志到flume

log4j.properties配置:


log4j.rootLogger=INFO
log4j.category.com.besttone=INFO,flume
log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
log4j.appender.flume.Hostname = localhost
log4j.appender.flume.Port = 44444

log4j.appender.flume.UnsafeMode = true

 

需要将/opt/cloudera/parcels/CDH-5.0.0-1.cdh5.0.0.p0.47/lib/flume-ng/tools/flume-ng-log4jappender-1.4.0-cdh5.0.0-jar-with-dependencies.jar添加到classpath下。

然后可以写一个简单的测试类来测试一下:

 

[java]  view plain copy 在CODE上查看代码片
 
  1. package com.besttone.flume;  
  2.   
  3. import java.util.Date;  
  4.   
  5. import org.apache.commons.logging.Log;  
  6. import org.apache.commons.logging.LogFactory;  
  7.   
  8. public class WriteLog {  
  9.     protected static final Log logger = LogFactory.getLog(WriteLog.class);  
  10.   
  11.     /** 
  12.      * @param args 
  13.      * @throws InterruptedException  
  14.      */  
  15.     public static void main(String[] args) throws InterruptedException {  
  16.         // TODO Auto-generated method stub  
  17.         while (true) {  
  18.         //每隔两秒log输出一下当前系统时间戳  
  19.             logger.info(new Date().getTime());  
  20.             Thread.sleep(2000);  
  21.         }  
  22.     }  
  23. }  


然后写一个run.sh脚本运行这个类:

[plain]  view plain copy 在CODE上查看代码片
 
  1. #!/bin/bash  
  2. jarlist=`ls ./lib/*.jar`  
  3. CLASSPATH='./bin/'  
  4. for jar in ${jarlist}  
  5. do  
  6.    CLASSPATH=${CLASSPATH}:${jar}  
  7. done  
  8. echo ${CLASSPATH}  
  9.   
  10. java -classpath "$CLASSPATH" com.besttone.flume.WriteLog &  


执行run.sh,将sink设置为logger,去flume的日志文件里去看,可以看到log4j的日志输出已经传输到了flume中:


2014-07-16 14:23:54,193 INFO org.apache.flume.sink.LoggerSink: Event: { headers:{flume.client.log4j.log.level=20000, flume.client.log4j.message.encoding=UTF8, flume.client.log4j.logger.name=com.besttone.flume.WriteLog, flume.client.log4j.timestamp=1405491834189} body: 31 34 30 35 34 39 31 38 33 34 31 38 39          1405491834189 }

你可能感兴趣的:(log4j)