Log4j可以很好的进行程序的跟踪:
1.首先编写log4j.properties,这个在网上有很多,可以直接拿过来使用
log4j.rootLogger=debug, stdout, R log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout # Pattern to output the caller's file name and line number. log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=./example.log log4j.appender.R.MaxFileSize= 100KB # Keep one backup file log4j.appender.R.MaxBackupIndex=1 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n
2.添加log4j-1.2.16.jar到工程中,如果不行也可添加logging-log4j-1.2.9.jar
3.写一个测试类:
import org.apache.log4j.Logger; public class Log4jTest { static Logger logger = Logger.getLogger(Log4jTest.class.getName()); public static void main(String[] args) { // BasicConfigurator replaced with PropertyConfigurator. logger.info("Entering application."); logger.info("Exiting application."); logger.debug("Exiting application."); } }?