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

log4j直接输出日志到flume

时间:2015-07-06 16:17:06      阅读:609      评论:0      收藏:0      [点我收藏+]

标签:

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


log4j直接输出日志到flume

标签:

原文地址:http://my.oschina.net/cloudcoder/blog/475171

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