在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>
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 | |
|
相关文章推荐
- 在有spring的b/s项目中配置log4j
- Log4j结合spring的使用配置
- log4j在spring中的配置
- SpringBoot配置log4j输出日志
- spring中配置log4j 基于maven项目
- springboot配置log4j
- spring boot默认日志配置,以及改用log4j日志配置
- 好记性不如烂笔头95-spring3学习(16)-spring配置log4j
- Spring MVC +Mybatis + Maven 配置之log4j配置
- 在Spring的项目中配置Log4j存放日志文件到指定目录下
- web 项目中 在spring中使用log4j 定时刷新log4j配置文件
- spring中使用log4j的配置
- spring boot 官方文档翻译之 配置log4j
- 在Spring的项目中配置Log4j
- spring中配置log4j
- spring boot 日志配置 log4j
- [SOA] Mule ESB 3.x 入门(二)—— 配置(spring, properties, log4j)
- spring boot log4j2配置(使用log4j2.yml文件)
- spring log4j配置
- Maven/Gradle 在Spring Boot中配置log4j