Log4j ---- 通过配置文件进行配置

log4j.properties

 

 

log4j.rootLogger = All,out1,out2,out3,out4
log4j.appender.out1 = org.apache.log4j.ConsoleAppender

log4j.appender.out2 = org.apache.log4j.FileAppender
log4j.appender.out2.File = forder/all.log

log4j.appender.out3 = org.apache.log4j.DailyRollingFileAppender
log4j.appender.out3.File = forder/day.log

log4j.appender.out4 = org.apache.log4j.RollingFileAppender
log4j.appender.out4.File = forder/max.log
log4j.appender.out4.MaxFileSize = 1kb


log4j.appender.out1.layout = org.apache.log4j.SimpleLayout
log4j.appender.out2.layout = org.apache.log4j.PatternLayout
log4j.appender.out3.layout = org.apache.log4j.PatternLayout
log4j.appender.out4.layout = org.apache.log4j.PatternLayout

log4j.appender.out2.layout.ConversionPattern = %d-[TS] %p %t %c - %m%n
log4j.appender.out3.layout.ConversionPattern = %d-[TS] %p %t %c - %m%n
log4j.appender.out4.layout.ConversionPattern = %d-[TS] %p %t %c - %m%n

 

 

 

 

 


 

 

 

 

 

 

package com.zwc.www.test;

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 OperateLog4j {

 private static Logger logger = Logger.getLogger(OperateLog4j.class.getName());
 public static void main(String[] args) {
  //BasicConfigurator.configure();
  Properties p = new Properties();
  try {
   p.load(new FileInputStream("properties/log4j.properties"));
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }
  PropertyConfigurator.configure(p);
  logger.info("Message");
 }
}

 

 

 

具体的细节说明,请参考上一篇文章,http://blog.csdn.net/yuanfen860913/archive/2010/05/25/5622998.aspx

阅读更多
版权声明:本文为博主原创文章,如若商业用途转载必须通知博主 https://blog.csdn.net/yuanfen860913/article/details/5623609
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭
关闭