LOG4J使用三部曲

LOG4J使用三部曲
1.编写一个配置文件在SRC目录下log4j.properties
2.导入log4j.jar包
3.写个Test类进行测试


#<!--log4j.properties-->
### direct log messages to stdout ###
log4j.rootLogger=ERROR,FILE


# 应用于文件回滚   
log4j.appender.FILE=org.apache.log4j.RollingFileAppender   
log4j.appender.FILE.Threshold=ERROR
#文件位置,也可以用变量${java.home}、bme.log    
log4j.appender.FILE.File=local/bme.log
#true:添加 false:覆盖  
log4j.appender.FILE.Append=true
#文件最大尺寸   
log4j.appender.FILE.MaxFileSize=1MB
#备份数 
log4j.appender.FILE.MaxBackupIndex=10
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout   
log4j.appender.FILE.layout.ConversionPattern=[%-d{MM-dd HH:mm:ss:SSS}][%t][%p][%c]- %m %n

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;


public class Test {
	
	
	static Logger LOGGER = Logger.getLogger(Test.class);
	
	static{
		Properties p = new Properties();
		try {
			p.load(new FileInputStream(new File("D:\\workspace3.2.1\\ss\\src\\log4j.properties")));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
			
		}
		PropertyConfigurator.configure(p);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Properties ps = new Properties();
		try {
			ps.load(new FileInputStream(new File("D:\\ss\\src\\log4j.properties")));
		} catch (FileNotFoundException e) {
			LOGGER.error(e);
			//e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			LOGGER.error(e);
			
		}	
		
		

	}

}

你可能感兴趣的:(java,apache,c,xml,log4j)