Log4J Appender - 将Log4J的日志内容发送到agent的source

项目中使用log4j打印的内容同时传输到flume


1、flume端

flume的agent配置内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
a1.sources=s1
a1.sinks=k1
a1.channels=c1
 
a1.sources.s1.channels=c1
a1.sinks.k1.channel=c1
 
a1.sources.s1.type=avro
a1.sources.s1.bind=vm1
a1.sources.s1.port=41414
 
a1.sinks.k1.type=logger
 
a1.channels.c1.type=memory
a1.channels.c1.capacity=1000
a1.channels.c1.transactionCapacity=100

启动这个agent

1
flume-ng agent --conf conf/ --conf-file conf/a1.conf --name a1 -Dflume.root.logger=INFO,console

2、java客户端

log4j.properties

1
2
3
4
5
log4j.rootLogger=INFO,flume
log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
log4j.appender.flume.Hostname = 192.168.1.111
log4j.appender.flume.Port = 41414
log4j.appender.flume.UnsafeMode = true

java测试代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package com.test.flume;
 
import org.apache.log4j.Logger;
 
public class Log4jAppenderTest {
 
    public static void main(String[] args) {
        Logger logger = Logger.getLogger(Log4jAppenderTest.class);
        logger.info("test successful");
        logger.error("error");
        System.out.println("ok");
    }
 
}

补充:

1、负载均衡的Log4J Appender

修改log4j的配置即可:

1
2
3
log4j.rootLogger=INFO,flume
log4j.appender.flume = org.apache.flume.clients.log4jappender.LoadBalancingLog4jAppender
log4j.appender.flume.Hosts = 192.168.1.111:41414 192.168.1.111:41414

不同的host用空格分隔

2、Log4J Appender也可以使用layout,如下:

1
2
3
4
5
6
7
8
9
10
11
12
log4j.rootLogger=INFO,console,flume
 
log4j.appender.flume = org.apache.flume.clients.log4jappender.Log4jAppender
log4j.appender.flume.Hostname = 192.168.1.111
log4j.appender.flume.Port = 41414
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,SSS} [%c]-[%p] %m%n
 
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%c]-[%p] %m%n








posted @ 2015-06-08 14:41  lishouguang  阅读(628)  评论(0编辑  收藏  举报