Log4j在web工程中应用

将Log4j的日志输出的web工程目录会方便系统移植、日志远程查看。那么如何来实现呢?可以通过一个自定义的Servlet设置系统属性的方法来实现,只需要几句代码,而且可配置、移植方便。
一、Servlet代码
Java代码   收藏代码
  1. package com.wallimn.gyz.util;  
  2. import javax.servlet.ServletException;  
  3. import javax.servlet.http.HttpServlet;  
  4. /** 
  5.  * 设置一些系统变量<br/> 
  6.  * 博客:http://wallimn.iteye.com 
  7.  * 编码:wallimn 时间:2009-9-25 下午12:16:27<br/> 
  8.  * 版本:V1.0<br/> 
  9.  */  
  10. public class SystemServlet extends HttpServlet {  
  11.   
  12.     private static final long serialVersionUID = 8164865597169685698L;  
  13.     /** 
  14.      * 读配置,设置系统变量 
  15.      */  
  16.     public void init() throws ServletException {  
  17.         String rootPath = this.getServletContext().getRealPath("/");  
  18.         String log4jPath = this.getServletConfig().getInitParameter("wallimn.log4j.path");  
  19.         //若没有指定wallimn.log4j.path初始参数,则使用WEB的工程目录  
  20.         log4jPath = (log4jPath==null||"".equals(log4jPath))?rootPath:log4jPath;  
  21.         System.setProperty("wallimn.log4j.path", log4jPath);  
  22.         super.init();  
  23.     }  
  24. }  

二、web.xml文件配置
<servlet>
<servlet-name>SystemServlet</servlet-name>
<servlet-class>com.wallimn.gyz.util.SystemServlet</servlet-class>
<init-param>
<param-name>wallimn.log4j.path</param-name>
<!--引自若未指定,则使用工程目录,若指定,使用指定目录-->
<param-value></param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>

三、log4j.properties文件配置(输出到工程目录下的logs子目录中)
log4j.appender.FILEOUT = org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILEOUT.File = ${wallimn.log4j.path}logs/log.html
log4j.appender.FILEOUT.Append = true
log4j.appender.FILEOUT.Threshold = DEBUG
log4j.appender.FILEOUT.layout = org.apache.log4j.HTMLLayout

网上流传的另一种方法:[color=brown][/color]

具体实现:编写一个 servlet, 在系统加载的时候, 就把 properties 的文件读到一个 properties 文件中。那个 file 的属性值(我使用的是相对目录)改掉(前面加上系统的根目录),然后把这个 properties 对象设置到 propertyConfig 中去,这样就初始化了 log 的设置。在后面的使用中就用不着再配置了。
一般在我们开发项目过程中,log4j 日志输出路径固定到某个文件夹,这样如果我换一个环境,日志路径又需要重新修改,比较不方便,目前我采用了动态改变日志路径的方法来实现相对路径保存日志文件
(1)  在项目启动时,装入初始化类:


public class Log4jInit extends HttpServlet {

         static Logger logger = Logger.getLogger(Log4jInit.class);

         public Log4jInit() {
         }

         public void init(ServletConfig config) throws ServletException {
             String prefix = config.getServletContext().getRealPath("/");
             String file = config.getInitParameter("log4j");
             String filePath = prefix + file;
             Properties props = new Properties();
             try {
                 FileInputStream istream = new FileInputStream(filePath);
                 props.load(istream);
                 istream.close();
                 //toPrint(props.getProperty("log4j.appender.file.File"));
                 String logFile = prefix + props.getProperty("log4j.appender.file.File");//设置路径
                    props.setProperty("log4j.appender.file.File",logFile);
                 PropertyConfigurator.configure(props);//装入log4j配置信息
             } catch (IOException e) {
                 toPrint("Could not read configuration file [" + filePath + "].");
                 toPrint("Ignoring configuration file [" + filePath + "].");
                 return;
             }
         }

         public static void toPrint(String content) {
             System.out.println(content);
         }
}

实际上 log4j 的配置文件如果为默认名称: log4j.properties,则可放置在 JVM 能读到的 classpath 里的任意地方,一般是放在 WEB-INF/classes 目录下。当log4j 的配置文件不再是默认名称,则需要另外加载并给出参数,如上 "PropertyConfigurator.configure(props);//装入log4j配置信息"。

(2)  web.xml 中的配置


      <servlet>
        <servlet-name>log4j-init</servlet-name>
        <servlet-class>Log4jInit</servlet-class>
        <init-param>
          <param-name>log4j</param-name>
          <param-value>WEB-INF/classes/log4j.properties</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>        </servlet>

注意:上面的 load-on-startup 设为 0 ,以便在 Web 容器启动时即装入该 Servlet 。log4j.properties 文件放在根的properties子目录中,也可以把它放在其它目录中。应该把 .properties 文件集中存放,这样方便管理。

(3)  log4j.properties 中即可配置 log4j.appender.file.File 为当前应用的相对路径
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值