log4j直接输出日志到flume

log4j直接输出日志到flume

        此jar是由Cloudera的CDH发行版提供的一个工具类,通过配置,可以将log4j的日志直接输出到flume,方便日志的采集。

        在CDH5.3.0版本中是:flume-ng-log4jappender-1.5.0-cdh5.3.0-jar-with-dependencies.jar

        所在目录是:/opt/cloudera/parcels/CDH/lib/flume-ng/tools/

具体使用示例

log4j配置(log4j.properties)

log4j.category.com.xxx=INFO,console,flume
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.target=System.out
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern="%d{yyyy-MM-dd HH:mm:ss} %p [%c:%L] - %m%n

log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
log4j.appender.flume.Hostname = localhost
log4j.appender.flume.Port = 4444
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} %p [%c:%L] - %m%n

配置classpath

        在classpath中要包含log4j.properties和flume-ng-log4jappender-1.5.0-cdh5.3.0-jar-with-dependencies.jar

编写JAVA测试类并导出为test.jar 

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);


    public static void main(String[] args) throws InterruptedException {
        // TODO Auto-generated method stub
        while (true) {
            // 每隔两秒log输出一下当前系统时间戳
            logger.info(new Date().getTime());
            Thread.sleep(2000);
            try {
                throw new Exception("exception msg");
            }
            catch (Exception e) {
                logger.error("error:" + e.getMessage());
            }
        }
    }
}

编写flume agent配置文件

a1.sources = r1
a1.sinks = k1
a1.channels = c1
 
# Describe/configure the source
a1.sources.r1.type = avro
a1.sources.r1.bind = localhost
a1.sources.r1.port = 4444
 
# Describe the sink
a1.sinks.k1.type = file_roll
a1.sinks.k1.sink.directory = /data/soft/flume/tmp
a1.sinks.k1.sink.rollInterval=86400
a1.sinks.k1.sink.batchSize=100
a1.sinks.k1.sink.serializer=text
a1.sinks.k1.sink.serializer.appendNewline = false
 
# Use a channel which buffers events in memory
a1.channels.c1.type = memory
a1.channels.c1.capacity = 1000
a1.channels.c1.transactionCapacity = 1000
 
# Bind the source and sink to the channel
a1.sources.r1.channels = c1
a1.sinks.k1.channel = c1

运行程序

        将相关程序上传到服务器,并先启动agent

        进入flume安装目录后,执行

        bin/flume-ng agent -c conf -f conf/avro.conf --name a1 -Dflume.root.logger=INFO,console

        执行测试程序

        java -classpath ./:flume-ng-log4jappender-1.5.0-cdh5.3.0-jar-with-dependencies.jar:test.jar com.xxx.WriteLog

检查运行结果

        tail -f /data/soft/flume/tmp/1436164166461-1 

2015-07-06 14:51:36 ERROR [com.xxx.WriteLog:27] - error:exception msg
2015-07-06 14:51:36 ERROR [com.xxx.WriteLog:28] - error:stack
2015-07-06 14:51:36 INFO [com.xxx.WriteLog:21] - 1436165496975
2015-07-06 14:51:38 ERROR [com.xxx.WriteLog:27] - error:exception msg
2015-07-06 14:51:38 ERROR [com.xxx.WriteLog:28] - error:stack
2015-07-06 14:51:38 INFO [com.xxx.WriteLog:21] - 1436165498977

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值