1.新建一个java project 项目
2.导入log4j的jar包,我用的是log4j-1.2.17.jar
3.新建log4j.properties文件,在src目录下:
内容如下:
log4j.rootLogger=debug,stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %p [%c] %m%n
4.新建一个class类
/**
*
*/
package com.log.test;import java.util.Properties;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;/**
* @author tucl
*
*/
public class LogTest {//Logger log=Logger.getLogger(LogTest.class);
public LogTest(){
//log.warn("yes yes yes yes---------");
}
private static void initLog4j() {
Properties prop = new Properties();prop.setProperty("log4j.rootLogger", "debug, CONSOLE");
prop.setProperty("log4j.appender.CONSOLE", "org.apache.log4j.ConsoleAppender");
prop.setProperty("log4j.appender.CONSOLE.layout", "org.apache.log4j.PatternLayout");
prop.setProperty("log4j.appender.CONSOLE.layout.ConversionPattern", "%d{HH:mm:ss,SSS} [%t] %-5p %C{1} : %m%n");PropertyConfigurator.configure(prop);
}
public static void main(String[] args) {
//PropertyConfigurator.configure("bin/log4j.properties");
PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties"));
//initLog4j();
//System.out.println("test");
Logger logger = Logger.getLogger(LogTest. class );
logger.debug( " debug " );
logger.debug("ceshi ce s s s s s");
logger.error( " error " );
}
}//PropertyConfigurator.configure("bin/log4j.properties");
PropertyConfigurator.configure(ClassLoader.getSystemResource("log4j.properties"));
//initLog4j();