Maven依赖:
<dependencies>
<dependency>
<groupId>org.apache.flume</groupId>
<artifactId>flume-ng-core</artifactId>
<version>1.5.2</version>
</dependency>
</dependencies>
Java代码:
package flume;
import java.text.SimpleDateFormat;
import org.apache.log4j.Logger;
public class LogProducer {
public static void main(String[] args) {
Logger log=Logger.getLogger(LogProducer.class);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
while (true) {
log.info("日志格式:"+sdf.format(System.currentTimeMillis()));
}
}
}
文件log4j.properties的内容
log4j.rootLogger=INFO,flume
log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
log4j.appender.flume.Hostname = 192.168.1.200
log4j.appender.flume.Port = 41414
log4j.appender.flume.UnsafeMode = true
日志中写Log4jAppender,就需要添加jar包,这个jar包是CDH的:
flume-ng-log4jappender-1.5.0-cdh5.1.3-jar-with-dependencies.jar
[root@baozi apache-flume-1.5.2-bin]# bin/flume-ng agent --conf conf/ -f conf/agent1.conf -n agent1 -Dflume.root.logger=DEBUG,console
2015-06-21 12:29:18,331 (New I/O worker #2) [DEBUG - org.apache.flume.source.AvroSource.append(AvroSource.java:347)] Avro
source as1: Received avro event: {"headers": {"flume.client.log4j.log.level": "20000", "flume.client.log4j.logger.name":
"flume.LogProducer", "flume.client.log4j.message.encoding": "UTF8", "flume.client.log4j.timestamp": "1434860959888"}, "body":
{"bytes": "???¥?????¥?????2015-06-21 12:29:19"}}
2015-06-21 12:29:18,332 (SinkRunner-PollingRunner-DefaultSinkProcessor) [INFO - org.apache.flume.sink.LoggerSink.process
(LoggerSink.java:70)] Event: { headers:{flume.client.log4j.log.level=20000, flume.client.log4j.message.encoding=UTF8,
flume.client.log4j.logger.name=flume.LogProducer, flume.client.log4j.timestamp=1434860959888} body: E6 97 A5 E5 BF 97 E6 A0 BC
E5 BC 8F EF BC 9A 32 ...............2 }