点击这里给我发消息 点击这里给我发消息

开源项目关于web app的Log4j应用

添加时间:2013-12-7
    相关阅读: 项目

  1 建立log.properties
  
  log level定为INFO,不显示DEBUG信息。log输出依次为文件,控制台
  
  log4j.rootLogger=INFO,R,CONSOLE#DEBUG,CONSOLElog4j.addivity.org.apache=true #################### Console Appender###################log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppenderlog4j.appender.Threshold=DEBUGlog4j.appender.CONSOLE.Target=System.outlog4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayoutlog4j.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.RollingFileAppenderlog4j.appender.R.File=d:\\file.loglog4j.appender.R.MaxFileSize=100KB# Keep one backup filelog4j.appender.R.MaxBackupIndex=1log4j.appender.R.layout=org.apache.log4j.PatternLayoutlog4j.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
  
  ublic 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 successfullyINFO http-8080-Processor24 com.nova.colimas.web.action.StartupServlet - init DAO successfullyINFO http-8080-Processor24 com.nova.colimas.web.action.LoginAction - login success
咨询热线:020-85648757 85648755 85648616 0755-27912581 客服:020-85648756 0755-27912581 业务传真:020-32579052
广州市网景网络科技有限公司 Copyright◎2003-2008 Veelink.com. All Rights Reserved.
广州商务地址:广东省广州市黄埔大道中203号(海景园区)海景花园C栋501室
= 深圳商务地址:深圳市宝源路华丰宝源大厦606
研发中心:广东广州市天河软件园海景园区 粤ICP备05103322号 工商注册