熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java開源技術 >> 正文

開源項目關於web app的Log4j應用

2013-11-23 20:27:53  來源: Java開源技術 

   建立logproperties
  
  log level定為INFO不顯示DEBUG信息log輸出依次為文件控制台
  
  logjrootLogger=INFORCONSOLE#DEBUGCONSOapache=true #################### Console Appender###################logjappenderCONSOLE=orgapachelogjConsoleAppenderlogjappenderThreshold=DEBUGlogjappenderCONSOLETarget=SystemoutlogjappenderCONSOLElayout=orgapachelogjPatternLayoutlogjappenderCONSOLElayoutConversionPattern=[framework] %d %c %r [%t] %p %c %x %m%n###################### File Appender文件滿kb自動生成filelogn#####################logjappenderR=orgapachelogjRollingFileAppenderlogjappenderRFile=d:\\fileloglogjappenderRMaxFileSize=KB# Keep one backup filelogjappenderRMaxBackupIndex=logjappenderRlayout=orgapachelogjPatternLayoutlogjappenderRlayoutConversionPattern=%p %t %c %m%n
  
   建立初始化Action Servlet
  
  import nstants*;import orgapachelogj*;public class StartupServlet extends Action { public ActionForward execute(ActionMapping mapping  ActionForm form  HttpServletRequest request  HttpServletResponse response) throws Exception{ try{//初始化log  initLog(); }catch(Exception e){  eprintStackTrace();  return mappingfindForward(failure); } Logger logger = LoggergetLogger(thisgetClass());  (init log); } private void initLog() throws Exception{//獲得logproperties絕對地址 URL myfile=thisgetClass()getResource(/resources/log/properties);//加載log配置文件logproperties if(myfilegetPath()!=null)  Propnfigure(myfilegetPath()); else  throw new Exception(no log configure); }}
  
   寫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 = LoggergetLogger(thisgetClass());   (login success);   return mappingfindForward(success);  }}
  
  log文件內容如下
  
  INFO cessor limaswebactionStartupServlet init security successfullyINFO Processor limaswebactionLoginAction login success
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28528.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.