1、依赖jar包导入,通过maven管理,pom文件增加如下配置:
<!-- log4j输出日志到flume的appender依赖包 --> <dependency> <groupId>org.apache.flume.flume-ng-clients</groupId> <artifactId>flume-ng-log4jappender</artifactId> <version>1.6.0</version> </dependency>
<!-- log4j 依赖包--> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.5</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>1.7.5</version> </dependency>2、log4j配置文件:
2.1 log4j.properties文件配置:
#log4j输出到flume单机模式配置 log4j.appender.loadbalance = org.apache.flume.clients.log4jappender.Log4jAppender log4j.appender.loadbalance.hostname = hadoop011 log4j.appender.loadbalance.port = 6666 #log4j输出到flume负载均衡方式配置 log4j.appender.loadbalance = org.apache.flume.clients.log4jappender.LoadBalancingLog4jAppender log4j.appender.loadbalance.Hosts = hadoop011:6666 hadoop012:6666 #log4j.appender.loadbalance.UnsafeMode = true log4j.appender.out2.MaxBackoff = 30000 #FQDN RANDOM ,default is ROUND_ROBIN log4j.appender.loadbalance.Selector = RANDOM log4j.appender.loadbalance.layout=org.apache.log4j.PatternLayout log4j.appender.loadbalance.layout.ConversionPattern=%m2.2 XMl方式配置:
<!-- 单机模式 --> <appender name="FLUME" class="org.apache.flume.clients.log4jappender.Log4jAppender"> <!-- 定义控制台日志级别入口 --> <param name="Hostname" value="192.168.12.11" /> <param name="Port" value="6666" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss,SSS} [%p][%t] %-20.30c{1} %4L %m%n" /> </layout> </appender> <!-- 负载均衡方式 --> <appender name="FLUME" class="org.apache.flume.clients.log4jappender.LoadBalancingLog4jAppender"> <!-- 定义控制台日志级别入口 --> <param name="hosts" value="192.168.12.11:6666 192.168.12.12:6666" /> <param name="Selector" value="RANDOM" /> <param name="maxBackoff" value="30000" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss,SSS} [%p][%t] %-20.30c{1} %4L %m%n" /> </layout> </appender>