1.导入依赖包jar
12 3 6 7 8 9org.apache.flume.flume-ng-clients 4flume-ng-log4jappender 1.6.0 510 14 15log4j 11log4j 121.2.17 1316 20 21org.slf4j 17slf4j-api 181.7.5 1922 26org.slf4j 23slf4j-log4j12 241.7.5 25
2.配置文件log4j.properties
1 # 2 # Licensed to the Apache Software Foundation (ASF) under one 3 # or more contributor license agreements. See the NOTICE file 4 # distributed with this work for additional information 5 # regarding copyright ownership. The ASF licenses this file 6 # to you under the Apache License, Version 2.0 (the 7 # "License"); you may not use this file except in compliance 8 # with the License. You may obtain a copy of the License at 9 #10 # http://www.apache.org/licenses/LICENSE-2.011 #12 # Unless required by applicable law or agreed to in writing,13 # software distributed under the License is distributed on an14 # "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY15 # KIND, either express or implied. See the License for the16 # specific language governing permissions and limitations17 # under the License.18 #19 20 # Define some default values that can be overridden by system properties.21 #22 # For testing, it may also be convenient to specify23 # -Dflume.root.logger=DEBUG,console when launching flume.24 25 #flume.root.logger=DEBUG,console26 flume.root.logger=INFO,DAILY27 flume.log.dir=./logs28 flume.log.file=flume.log29 30 31 # Define the root logger to the system property "flume.root.logger".32 log4j.rootLogger=${flume.root.logger}33 34 35 # Stock log4j rolling file appender36 # Default log rotation configuration37 #按文件大小回滚RollingFileAppender38 log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender39 #当产生日志文件大于MaxFileSize,进行回滚40 log4j.appender.LOGFILE.MaxFileSize=20MB41 #最大回滚数42 log4j.appender.LOGFILE.MaxBackupIndex=3043 #产生日志文件的路径及文件名44 log4j.appender.LOGFILE.File=${flume.log.dir}/${flume.log.file}45 #自定义格式46 log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout47 log4j.appender.LOGFILE.layout.ConversionPattern=%d{dd MMM yyyy HH:mm:ss,SSS} %-5p [%t] (%C.%M:%L) %x - %m%n48 49 50 # Warning: If you enable the following appender it will fill up your disk if you don't have a cleanup job!51 # This uses the updated rolling file appender from log4j-extras that supports a reliable time-based rolling policy.52 # See http://logging.apache.org/log4j/companions/extras/apidocs/org/apache/log4j/rolling/TimeBasedRollingPolicy.html53 54 # console55 # Add "console" to flume.root.logger above if you want to use this56 log4j.appender.console=org.apache.log4j.ConsoleAppender57 log4j.appender.console.target=System.err58 log4j.appender.console.layout=org.apache.log4j.PatternLayout59 log4j.appender.console.layout.ConversionPattern=%d (%t) [%p - %l] %m%n
把上面的log4j配置文件放在flume的conf目录下
3. 启动flume
使用命令:./bin/flume-ng agent --conf conf --conf-file conf/flume2CustomSink.conf --name a1
注意:不要在命令后面加-Dflume.root.logger=INFO,console 即不要用这命令:./bin/flume-ng agent --conf conf --conf-file conf/flume2CustomSink.conf --name a1 -Dflume.root.logger=INFO,console
因为使用这个命令就修改了flume.root.logger的值,变成打印在控制台上了。这和我们要打印成日志文件的目的相违背。除非你想要在控制台看打印信息。
4.运行成功后可以看到在flume根目录下生成了 ./logs/flume.log 文件
关于flume的安装和部署 请参照:
最后附上log4jPatternLayout输出格式:
ConversionPattern属性:
%m 输出代码中指定的消息;
%M 输出打印该条日志的方法名;
%p 输出优先级,即DEBUG,,WARN,ERROR,FATAL;
%r 输出自应用启动到输出该log信息耗费的毫秒数;
%c 输出所属的类目,通常就是所在类的全名;
%t 输出产生该日志事件的线程名;
%n 输出一个回车换行符,Windows平台为"rn”,Unix平台为"n”;
%d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyyy-MM-dd HH:mm:ss,SSS},输出类似:2002-10-18 22:10:28,921;
%l 输出日志事件的发生位置,及在代码中的行数;