码迷,mamicode.com
首页 > Web开发 > 详细

log4j直接输出日志到flume

时间:2015-10-13 20:52:24      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

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 plaincopy技术分享技术分享
 
  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 plaincopy技术分享技术分享
 
  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直接输出日志到flume

标签:

原文地址:http://www.cnblogs.com/AloneSword/p/4875671.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!