Log4j Direct output log to Flume

Source: Internet
Author: User

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
  1. Package com.besttone.flume;
  2. Import Java.util.Date;
  3. Import Org.apache.commons.logging.Log;
  4. Import Org.apache.commons.logging.LogFactory;
  5. Public class Writelog {
  6. protected static final Log logger = Logfactory.getlog (writelog.   Class);
  7. /** 
  8. * @param args
  9. * @throws interruptedexception
  10. */
  11. public static void Main (string[] args) throws interruptedexception {
  12. //TODO auto-generated method stub
  13. While (true) {
  14. //Every two seconds log output current system timestamp
  15. Logger.info (new Date (). GetTime ());
  16. Thread.Sleep (2000);
  17. }
  18. }
  19. }


Then write a run.sh script to run this class:

[Plain]View Plaincopy
    1. #!/BIN/BASH  
    2. jarlist= ' Ls ./lib/*.jar '   
    3. Li class= "alt" >classpath= './bin/'   
    4. for jar in ${jarlist}  
    5. DO  
    6.    classpath=${classpath}:${jar}  
    7. DONE  
    8. echo ${classpath}  
    9.   
    10. 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.