到 Google 资讯主页   
EasyJF首页   资料   源码   软件    论坛   网站    
   使用帮助    
    该信息为本站MyRSS系统缓存内容,部分图片及附件有可能无法正常使用.easyjf.comwww.matrix.org.cn无关,不对该信息负责.通过http://www.matrix.org.cn//resource/article/43/43601_Log4j.html访问该信息的原始内容.
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   
web app的Log4j应用
作者:tyrone1979 来源:www.matrix.org.cn  发布时间:2006-02-22 17:51:40.407

web app的Log4j应用
1 建立log.properties
log level定为INFO,不显示DEBUG信息。log输出依次为文件,控制台

log4j.rootLogger=INFO,R,CONSOLE
#DEBUG,CONSOLE
log4j.addivity.org.apache=true

###################
# Console Appender
###################
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.Threshold=DEBUG
log4j.appender.CONSOLE.Target=System.out
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n
#####################
# File Appender,文件满100kb,自动生成file.log.n
#####################
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=d:\\file.log
log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n



2 建立初始化Action Servlet

import com.nova.colimas.web.constants.*;
import org.apache.log4j.*;
public class StartupServlet extends Action {
public ActionForward execute(ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
throws Exception{
  try{
//初始化log
   initLog();
  }catch(Exception e){
   e.printStackTrace();
   return mapping.findForward("failure");
  }
  Logger logger = Logger.getLogger(this.getClass());
  logger.info("init log...");
  }
  private void initLog() throws Exception{
//获得log.properties绝对地址
  java.net.URL myfile=this.getClass().getResource("/resources/log/properties");
//加载log配置文件log.properties
  if(myfile.getPath()!=null)
   PropertyConfigurator.configure(myfile.getPath());
  else
   throw new Exception("no log configure");
}
}


3 写log
public class LoginAction extends Action {
        
        LoginContext loginContext=null;
        LoginForm loginForm=null;
        public ActionForward execute(ActionMapping mapping,
                         ActionForm form,
                         HttpServletRequest request,
                         HttpServletResponse response)
        throws Exception{
                    Logger logger = Logger.getLogger(this.getClass());
          logger.info("login success");
      return mapping.findForward("success");
        }
}


log文件内容如下:
INFO http-8080-Processor24 com.nova.colimas.web.action.StartupServlet - init colimas...
INFO http-8080-Processor24 com.nova.colimas.web.action.StartupServlet - init security successfully
INFO http-8080-Processor24 com.nova.colimas.web.action.StartupServlet - init DAO successfully
INFO http-8080-Processor24 com.nova.colimas.web.action.LoginAction - login success

 
相关文章
 
页面功能  【加入收藏】 【推荐给朋友】 【字体:  】 【关闭】   


EasyJF.com 2006 隐私政策 使用EasyJF前必读