文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>spring中配置log4j

spring中配置log4j

时间:2010-04-12  来源:sunwei0325

首先需要在web.xml进行声明:

<!--如果不定义webAppRootKey参数,那么webAppRootKey就是缺省的"webapp.root"-->
<context-param>
   <param-name>webAppRootKey</param-name>
   <param-value>ssh.root</param-value>
</context-param>

这里的ssh是http://localhost:8080/ssh/

而root是系统开发目录中ssh/root

这样配置文件以及日志文件可以在spring的帮助下,放到开发环境中的任意位置

<!--由Sprng载入的Log4j配置文件位置-->
<context-param>
   <param-name>log4jConfigLocation</param-name>
   <param-value>/WEB-INF/log4j.properties</param-value>
</context-param>

在这里定位配置文件,需要的是从root开始的绝对路径

<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond-->
<context-param>
   <param-name>log4jRefreshInterval</param-name>
   <param-value>60000</param-value>
</context-param>

<!--Spring log4j Config loader-->
<listener>
   <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

设置监听器



/////////////////
之后我们就可以配置log4j配置文件了

#先设置级别
log4j.rootCategory=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#在这里设置日志需要存放的位置,这里的变量就是我们在web.xml里设置的
log4j.appender.file.File=${ssh.root}/WEB-INF/logs/subject.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.MaxBackupIndex=0
log4j.appender.file.layout=org.apache.log4j.SimpleLayout
log4j.appender.file.layout.ConversionPattern=[ssh] %p %t %c - %m%n

有了上面的配置,我们就可以查看日志了!

示例代码:

下面是代码
----------------------------------------------------------------------------------------------------
package com.yourcompany.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.yourcompany.struts.form.UserForm;
public class UserAction extends Action {
    private static Logger logger = Logger.getLogger(UserAction.class);
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) {
        UserForm userForm = (UserForm) form;
        logger.info("用户登录:");
        try {
            throw new Exception("有异常");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.toString());
        }
        return null;
    }
}
----------------------------------------------------------------------------------------------------

log4j.properties
----------------------------------------------------------------------------------------------------

#先设置级别
log4j.rootCategory=INFO, file
log4j.appender.file=org.apache.log4j.RollingFileAppender
#在这里设置日志需要存放的位置,这里的变量就是我们在web.xml里设置的
log4j.appender.file.File=${log.root}/WEB-INF/logs/subject.log
log4j.appender.file.MaxFileSize=100KB
log4j.appender.file.MaxBackupIndex=0
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[log] %p %t %c (%F:%L) - %m%n

----------------------------------------------------------------------------------------------------

web.xml
----------------------------------------------------------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <context-param>
        <param-name>webAppRootKey</param-name>
        <param-value>log.root</param-value>
    </context-param>


    <!--由Sprng载入的Log4j配置文件位置-->
    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/log4j.properties</param-value>
    </context-param>

    <servlet>

        <servlet-name>action</servlet-name>
        <servlet-class>
            org.apache.struts.action.ActionServlet
        </servlet-class>
        <init-param>
            <param-name>config</param-name>
            <param-value>/WEB-INF/struts-config.xml</param-value>
        </init-param>
        <init-param>
            <param-name>debug</param-name>
            <param-value>3</param-value>
        </init-param>
        <init-param>
            <param-name>detail</param-name>
            <param-value>3</param-value>
        </init-param>
        <load-on-startup>0</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>action</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>

    <!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond-->
    <context-param>
        <param-name>log4jRefreshInterval</param-name>
        <param-value>60000</param-value>
    </context-param>

    <!--Spring log4j Config loader-->
    <listener>
        <listener-class>
            org.springframework.web.util.Log4jConfigListener
        </listener-class>
    </listener>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载