linux log4j 使用

1、首先到Apache官网下载log4j.jar文件http://logging.apache.org/log4j/1.2/download.html 引用到eclipse项目里面

2、在src目录下建立:log4j.properties

log4j.rootCategory=debug, stdout,R 

 

log4j.appender.stdout=org.apache.log4j.ConsoleAppender 

 

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 

 

log4j.appender.stdout.layout.ConversionPattern=%5p (%F:%L) - %m%n 

 

log4j.appender.R=org.apache.log4j.DailyRollingFileAppender 

 

log4j.appender.R.File=log/log.log 

 

# 如果为 (每天产生日志就不需要设置下面属性)DailyRollingFileAppender

#log4j.appender.R.MaxFileSize=100KB 

 

#log4j.appender.R.MaxBackupIndex=1 

 

log4j.appender.R.layout=org.apache.log4j.PatternLayout 

 

log4j.appender.R.layout.ConversionPattern=%d{yyyy MMM dd HH:mm:ss} %-5p %c - %m%n 

3、使用

public class Main   {
    static Logger logger = Logger.getLogger(Main.class.getName()); 
        public static void main(String[] args) throws IOException {
        // TODO Auto-generated method stub
         System.out.println("Hello World!"); 
         PropertyConfigurator.configure ("src//log4j.properties");  
          
            logger.debug("Debug ...");  
      
            logger.info("Info ...");  
      
            logger.warn("Warn ...");  
      
            logger.error("Error ...");  
       
    }

}

 

posted on 2014-11-14 10:38  lvlv岁月流逝  阅读(2001)  评论(0编辑  收藏  举报

导航