本文摘自:http://www.blogjava.net/jacky9881/archive/2008/01/02/172086.html
1.我们已经搭建完了基本框架,接下来加入日志功能,只需修改原来的web.xml,增加一些配置信息:
1<!--通知Spring Log4j配置文件的位置 --> 2<context-param> 3 <param-name>log4jConfigLocation</param-name> 4 <param-value>/WEB-INF/log4j.properties</param-value> 5</context-param> 6 7<!--Spring默认刷新Log4j配置文件的间隔,单位为millisecond--> 8<context-param> 9 <param-name>log4jRefreshInterval</param-name> 10 <param-value>60000</param-value> 11</context-param> 12 13<!--在应用启动时,由Spring负责加载Log4j--> 14<listener> 15 <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 16</listener>
2.接下来我们定义log4j.properties
1# Global logging configuration 2log4j.rootLogger=DEBUG,console,file 3 4## APPENDERS ## 5# define an appender named console, which is set to be a ConsoleAppender 6log4j.appender.console=org.apache.log4j.ConsoleAppender 7 8# define an appender named file, which is set to be a RollingFileAppender 9log4j.appender.file=org.apache.log4j.RollingFileAppender 10# replace <CATALINA_HOME> with your true path 11log4j.appender.file.File=D:/eclipse/workspace/login/log.txt 12 13## LAYOUTS ## 14# assign a SimpleLayout to console appender 15log4j.appender.console.layout=org.apache.log4j.SimpleLayout 16 17# assign a PatternLayout to file appender 18log4j.appender.file.layout=org.apache.log4j.PatternLayout 19log4j.appender.file.layout.ConversionPattern=%t%p-%m%n
在log4j.properties中,我们定义了日志级别是DEBUG,日志信息输出目的地是控制台(console)和文件(file)。
3.这样我们就可以在程序代码中进行日志操作了:
1package test.register.action; 2 3import javax.servlet.http.HttpServletRequest; 4import javax.servlet.http.HttpServletResponse; 5 6import org.apache.log4j.LogManager; 7import org.apache.log4j.Logger; 8import org.apache.struts.action.Action; 9import org.apache.struts.action.ActionForm; 10import org.apache.struts.action.ActionForward; 11import org.apache.struts.action.ActionMapping; 12import org.apache.struts.action.DynaActionForm; 13 14import test.register.entity.Register; 15import test.register.service.RegisterService; 16 17public class RegisterAction extends Action{ 18 19 private static final Logger log = LogManager.getLogger(RegisterAction.class); 20 21 private RegisterService registerService; 22 23 public RegisterService getRegisterService() { 24 return registerService; 25 } 26 27 public void setRegisterService(RegisterService registerService) { 28 this.registerService = registerService; 29 } 30 31 public ActionForward execute(ActionMapping mapping, 32 ActionForm form, 33 HttpServletRequest request, 34 HttpServletResponse response) 35 throws Exception { 36 log.info("save register"); 37 Register r = (Register)((DynaActionForm)form).get("register"); 38 getRegisterService().saveRegister(r); 39 return mapping.findForward("success"); 40 } 41 42}
4.在上面log4j.properties中,我们指定日志输入文件用了绝对路径,这在很多时候不方便,为此我们作如下修改:在web.xml中增加一个上下文参数
1<context-param> 2 <param-name>webAppRootKey</param-name> 3 <param-value>webLogin.root</param-value> 4</context-param>
增加系统变量,变量名为webAppRootKey,变量值可以指定应用部署的目录,这样就可以在log4j.properties对日志输出的文件路径作如下修改:
log4j.appender.file.File=${webLogin.root}/log.txt