WEB系统启动时加载Log4j的配置文件(自己写Listener形式)

在整个WEB系统中,为了统一的使用日志管理,需要在系统启动的时候就加载Log4j的配置文件,这样才能保证以后使用log4j的格式是一致的,便于跟踪和解决问题。

那么,如何在系统启动的时候加载log4j的配置文件呢?下面我简单的介绍一下:

 

1、在web.xml文件中添加一个“监听器”

Xml代码   收藏代码
  1. <!-- 加载log4j的配置信息 -->  
  2.   <listener>  
  3.     <listener-class>hb.init.log4j.Log4jInit</listener-class>  
  4.   </listener>  

 

2、“监听类”继承“ServletContextListener”接口

Java代码   收藏代码
  1. package hb.init.log4j;  
  2.   
  3. import java.io.FileInputStream;  
  4. import java.io.IOException;  
  5. import java.util.Properties;  
  6. import javax.servlet.ServletContext;  
  7. import javax.servlet.ServletContextEvent;  
  8. import javax.servlet.ServletContextListener;  
  9. import org.apache.log4j.Logger;  
  10. import org.apache.log4j.PropertyConfigurator;  
  11.   
  12. public class Log4jInit implements ServletContextListener{  
  13.       
  14.     Logger log = Logger.getLogger(Log4jInit.class);  
  15.       
  16.     public void contextDestroyed(ServletContextEvent sce) {  
  17.         log.info("Log4jInit contextDestroyed!");  
  18.     }  
  19.   
  20.     public void contextInitialized(ServletContextEvent sce) {  
  21.           
  22.         //得到servletContext对象的方法  
  23.         ServletContext sc = sce.getServletContext();  
  24.         //指明文件的相对路径就能够得到文件的绝对路径  
  25.         System.out.println(sc.getRealPath("/"));  
  26.         String path = sc.getRealPath("/config/log4j.properties");  
  27.           
  28.         //启动服务器的时候加载日志的配置文件  
  29.         init(path,sc);  
  30.         log.info("log4j");  
  31.     }  
  32.       
  33.       
  34.     /** 
  35.      *  
  36.      * @param path 配置文件的路径 
  37.      * @param sc ServletContext对象 
  38.      */  
  39.     public void init(String path,ServletContext sc){  
  40.         FileInputStream istream = null;  
  41.         try{  
  42.             Properties props = new Properties();  
  43.             //加载配置文件  
  44.             istream = new FileInputStream(path);  
  45.             props.remove("log4j.appender.file.File");  
  46.             System.out.println(sc.getRealPath("/log/hb.log"));  
  47.             //指明log文件的位置  
  48.             props.put("log4j.appender.file.File", sc.getRealPath("/log/hb.log"));  
  49.             //加载文件流,加载Log4j文件的配置文件信息  
  50.             props.load(istream);  
  51.             PropertyConfigurator.configure(props);  
  52.         } catch (Exception ex){  
  53.             try {  
  54.                 throw new Exception(ex);  
  55.             } catch (Exception e) {  
  56.                 e.printStackTrace();  
  57.             }  
  58.         } finally{  
  59.             try {  
  60.                 istream.close();  
  61.             } catch (IOException e) {  
  62.                 e.printStackTrace();  
  63.             }  
  64.         }  
  65.     }  
  66.       
  67.       
  68. }  

 

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


http://hbiao68.iteye.com/blog/1570421

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值