您的位置:首页 > 编程语言 > Java开发

在Spring中配置log4j

2009-07-03 12:49 417 查看
在Spring中配置log4j
2008年05月19日 星期一 10:32
----------------------------------------------------------------------------------------------------
在Spring中配置log4j

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

记得导入Spring web lib
spring-web.jar

----------------------------------------------------------------------------------------------------
首先需要在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>

[Spring]在Spring中配置log4j
软件技术

lhwork 发表于 2006-8-4 10:00:02

首先需要在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

有了上面的配置,我们就可以查看日志了!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: