log4j

导包:log4j-1.2.14.jar 

写配置文件:在CLASSPATH下创建log4j.properties:

log4j.rootLogger=info, ServerDailyRollingFile, stdout 
log4j.appender.ServerDailyRollingFile=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.ServerDailyRollingFile.DatePattern='.'yyyy-MM-dd 
log4j.appender.ServerDailyRollingFile.File=C://logs/notify-subscription.log 
log4j.appender.ServerDailyRollingFile.layout=org.apache.log4j.PatternLayout 
log4j.appender.ServerDailyRollingFile.layout.ConversionPattern=%d - %m%n 
log4j.appender.ServerDailyRollingFile.Append=true


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

测试类:

public class test {
static Logger log = Logger.getLogger(test.class);
public test(){
log.warn("ha ha ha !");
}
public static void main(String[] args){
PropertyConfigurator.configure("log4j.properties");
SimpleLayout layout = new SimpleLayout();
FileAppender appender =  null;
try{
appender = new FileAppender(layout,"output1.txt",false);

}catch(Exception e){
e.printStackTrace();
}
log.addAppender(appender);
log.setLevel((Level)Level.DEBUG);
log.debug("here is some debug!");
log.info("here is some info");
log.warn("here is some warn警告");
log.error("here is some error");
log.fatal("here is some fatal");
test t = new test();
}
}


你可能感兴趣的:(个人类型)