Log4j.properties configuration:
Log4j.rootlogger=info
Log4j.category.com.besttone=info,flume
Log4j.appender.flume = Org.apache.flume.clients.log4jappender.Log4jAppender
Log4j.appender.flume.Hostname = localhost
Log4j.appender.flume.Port = 44444
Log4j.appender.flume.UnsafeMode = True
Need to be/opt/cloudera/parcels/cdh-5.0.0-1.cdh5.0.0.p0.47/lib/flume-ng/tools/ Flume-ng-log4jappender-1.4.0-cdh5.0.0-jar-with-dependencies.jar added to Classpath.
You can then write a simple test class to test:
[Java]View Plaincopy
- Package com.besttone.flume;
- 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);
- /**
- * @param args
- * @throws interruptedexception
- */
- public static void Main (string[] args) throws interruptedexception {
- //TODO auto-generated method stub
- While (true) {
- //Every two seconds log output current system timestamp
- Logger.info (new Date (). GetTime ());
- Thread.Sleep (2000);
- }
- }
- }
Then write a run.sh script to run this class:
[Plain]View Plaincopy
- #!/BIN/BASH  
- jarlist= ' Ls ./lib/*.jar '
Li class= "alt" >classpath= './bin/'
- for jar in ${jarlist}
- DO  
- classpath=${classpath}:${jar}
- DONE  
- echo ${classpath}
-   
- java -classpath "$CLASSPATH"  COM.BESTTONE.FLUME.WRITELOG &  
Execute run.sh, set sink to logger, go to flume log file to see, you can see the log4j log output has been transferred to the flume:
2014-07-16 14:23:54,193 INFO org.apache.flume.sink.LoggerSink:Event: {headers:{flume.client.log4j.log.level=20000, Flume.client.log4j.message.encoding=utf8, Flume.client.log4j.logger.name=com.besttone.flume.writelog, flume.client.log4j.timestamp=1405491834189} body:31 34 30 35 34 39 31 38 33 34 31 38 39 1405491834189}
Log4j Direct output log to Flume