WEB系统启动时加载Log4j的配置文件

  如何在系统启动的时候加载log4j的配置文件呢?

  1、自定义监听类并且继承“ServletContextListener”接口:

  

 1 package cn.ibeans.common;
 2 
 3 import java.io.File;
 4 import java.util.List;
 5 
 6 import javax.servlet.ServletContextEvent;
 7 import javax.servlet.ServletContextListener;
 8 
 9 import org.apache.log4j.Logger;
10 import org.apache.log4j.PropertyConfigurator;
11 import cn.ibeans.common.util.FileUtil;
12 /**
13  * 
14  * @author hezuoan
15  *
16  */
17 public class ApplicationListener implements ServletContextListener {
18     
19     private static Logger log = Logger.getLogger(ApplicationListener.class);
20     @Override
21     public void contextInitialized(ServletContextEvent sce) {
22          
23         //获取log4j配置文件的地址
24         String log4jPath = ApplicationDispatchServlet.class.getResource("/").getPath() + "config/logs";
25         List<File> files = FileUtil.listFile(new File(log4jPath), "properties", false);
26         
27         if ((files == null) || (files.size() == 0)){
28           log.info("没有发现Log4j配置文件.");
29           return;
30         }
31         for (File file : files) {
32             //加载配置文件
33           PropertyConfigurator.configure(file.getPath());
34         }
35         log.info("加载Log4j配置文件完成.");
36     }
37 
38     @Override
39     public void contextDestroyed(ServletContextEvent sce) { }
40 
41 }

  上述代码中FileUtil.listFile() 方法是自己封装的工具类。

 1  /**
 2    * 获取文件夹下的所有文件
 3    * @param dir    文件夹路径
 4    * @param filename 后缀名称
 5    * @param recursive 是否递归该文件夹下的子文件夹
 6    * @return 
 7    */
 8   public static List<File> listFile(File dir,final String filename, boolean recursive)
 9   {
10     if (!dir.exists()) {
11       throw new IllegalArgumentException("目录:" + dir + "不存在");
12     }
13     if (!dir.isDirectory()) {
14       throw new IllegalArgumentException(dir + "不是目录");
15     }
16     FileFilter ff = null;
17     if ((filename == null) || (filename.length() == 0)) {
18       ff = new FileFilter()
19       {
20         public boolean accept(File pathname)
21         {
22           return true;
23         }
24       };
25     } else {
26       ff = new FileFilter()
27       {
28         public boolean accept(File pathname)
29         {
30           if (pathname.isDirectory()) {
31             return true;
32           }
33           String name = pathname.getName();
34           if (name.indexOf(filename) != -1) {
35             return true;
36           }
37           return false;
38         }
39       };
40     }
41     return listFile(dir, ff, recursive);
42   }
43   
44   private static List<File> listFile(File dir, FileFilter ff, boolean recursive)
45   {
46     List<File> list = new ArrayList<File>();
47     File[] subs = dir.listFiles(ff);
48     if ((subs != null) && (subs.length > 0)) {
49       for (File sub : subs) {
50         if (sub.isFile()) {
51           list.add(sub);
52         } else if (recursive) {
53           list.addAll(listFile(sub, ff, true));
54         }
55       }
56     }
57     return list;
58   }
View Code

 2、在Web.xml 添加该监听器的配置:

  

   <listener>
         <listener-class>cn.ibeans.common.ApplicationListener</listener-class>
     </listener> 

 

加载log4j的配置文件的目的是为了使日志文件“放在跟工程相对路径的地方”,这样即使将项目移植到不同的操作系统上面,显示也是正常的。

 

posted @ 2014-09-09 15:37  贺佐安  阅读(5274)  评论(0编辑  收藏  举报