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下。
然后可以写一个简单的测试类来测试一下:
- package com.besttone.flume;
- import java.util.Date;
- import org.apache.commons.logging.Log;
- import org.apache.commons.logging.LogFactory;
- public class WriteLog {
- protected static final Log logger = LogFactory.getLog(WriteLog.class);
- /**
- * @param args
- * @throws InterruptedException
- */
- public static void main(String[] args) throws InterruptedException {
- // TODO Auto-generated method stub
- while (true) {
- //每隔两秒log输出一下当前系统时间戳
- logger.info(new Date().getTime());
- Thread.sleep(2000);
- }
- }
- }
然后写一个run.sh脚本运行这个类:
- #!/bin/bash
- jarlist=`ls ./lib/*.jar`
- CLASSPATH='./bin/'
- for jar in ${jarlist}
- do
- CLASSPATH=${CLASSPATH}:${jar}
- done
- echo ${CLASSPATH}
- 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 }