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