Pure Java configuration log4j logs in eclipse

Source: Internet
Author: User
Tags log4j

1. New Java Project Log4test

2, New directory lib, the Log4j-1.2.9.jar package into the Lib directory

3, right-click Project, choose Properties->java Build path->libraries->add JARs ..., select the Log4j-1.2.9.jar under Lib

4. Create two new files in src directory log4j.properties and Log.txt

The Log4j.properties file is configured as follows:

log4j.appender.stdout.layout.conversionpattern=%d%p [%c]-%m%n
Log4j.appender.logfile=org.apache.log4j.fileappender
Log4j.appender.logfile.file=target/spring.log
Log4j.appender.logfile.layout=org.apache.log4j.patternlayout
log4j.appender.logfile.layout.conversionpattern=%d%p [%c]-%m%n

Log4j.appender.stdout=org.apache.log4j.consoleappender
Log4j.appender.stdout.target=system.out
Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
Log4j.appender.stdout.layout.conversionpattern=%d{yyyy/mm/dd HH:mm:ss:SSS}%5p%c:%l-%m%n

Log4j.appender.file=org.apache.log4j.fileappender

#实际项目路径要对应上
Log4j.appender.file.file=e:/workspace/log4test/src/log.txt
Log4j.appender.file.layout=org.apache.log4j.patternlayout
Log4j.appender.file.layout.conversionpattern=%d{yyyy/mm/dd HH:mm:ss:SSS}%5p%c:%l-%m%n

Log4j.rootlogger=info,file

5, the new Logtest test class, as follows

 package   Com.test;  import   Org.apache.log4j.Logger;  public  class   Logtest { public  static  Logger Logger = Logger.getlogger (Test.class  );  public  static  void   main (string[] args) { // 
   
     TODO auto-generated Method stub  String log= "log4 log Test" 
    
   

6, run Logtest, write the following information in the Log.txt file:

2015/09/04 09:37:03:650 INFO com.test.test:11-log4 Log test

Pure Java configuration log4j logs in eclipse

Related Article

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.