春風(fēng)博客

          春天里,百花香...

          導(dǎo)航

          <2008年2月>
          272829303112
          3456789
          10111213141516
          17181920212223
          2425262728291
          2345678

          統(tǒng)計(jì)

          公告

          MAIL: junglesong@gmail.com
          MSN: junglesong_5@hotmail.com

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個(gè)人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          Tomcat工程中的log4j配置

          1)log4j.properties文件內(nèi)容如下,此文件要配置到ClassPath中去。
          log4j.rootLogger=debug, stdout, R

          log4j.appender.stdout=org.apache.log4j.ConsoleAppender
          log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

          # Pattern to output the caller's file name and line number.
          log4j.appender.stdout.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n

          log4j.appender.R=org.apache.log4j.RollingFileAppender
          log4j.appender.R.File=AccountBook.log

          log4j.appender.R.MaxFileSize=1000KB
          # Keep one backup file
          log4j.appender.R.MaxBackupIndex=1

          log4j.appender.R.layout=org.apache.log4j.PatternLayout
          log4j.appender.R.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n

          2)書寫一個(gè)初始化Log4j的servlet如下:
          public class Log4jInit extends HttpServlet {
              
          private static final long serialVersionUID = -4499302208753939187L;
              
          static Logger logger = Logger.getLogger(Log4jInit.class);

               
          public void init(ServletConfig config) throws ServletException {
                   String prefix 
          = config.getServletContext().getRealPath("/");
                   String file 
          = config.getInitParameter("log4j");
                   String filePath 
          = prefix + file;
                   Properties props 
          = new Properties();
                   
                   
          try {
                       FileInputStream istream 
          = new FileInputStream(filePath);
                       props.load(istream);
                       istream.close();

                       String logFile 
          = prefix + props.getProperty("log4j.appender.R.File");//設(shè)置路徑
                       props.setProperty("log4j.appender.R.File",logFile);
                       
                       
          // 裝入log4j配置信息
                       PropertyConfigurator.configure(props);
                   } 
          catch (IOException e) {
                       System.out.println(
          "Could not read configuration file [" + filePath + "].");
                       System.out.println(
          "Ignoring configuration file [" + filePath + "].");
                       
          return;
                   }
               }
          }

          3)在Web.xml中配置Log4jInit如下:
              <servlet>
                   
          <servlet-name>log4j-init</servlet-name>
                   
          <servlet-class>
                       com.sitinspring.action.Log4jInit
                   
          </servlet-class>
                   
          <init-param>
                     
          <param-name>log4j</param-name>
                     
          <param-value>WEB-INF/classes/log4j.properties</param-value>
                   
          </init-param>
                   
          <load-on-startup>1</load-on-startup>
              
          </servlet>

          此后配置文件就出現(xiàn)在工程目錄下。

          posted on 2008-02-16 13:53 sitinspring 閱讀(2613) 評(píng)論(0)  編輯  收藏 所屬分類: Java基礎(chǔ)

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處.
          主站蜘蛛池模板: 紫阳县| 竹北市| 盐津县| 大同县| 本溪市| 安平县| 特克斯县| 革吉县| 宿松县| 梁河县| 古浪县| 土默特左旗| 临湘市| 城固县| 得荣县| 丘北县| 钟祥市| 密山市| 黎城县| 民丰县| 黑龙江省| 张家口市| 射洪县| 淮北市| 岫岩| 册亨县| 南和县| 广河县| 长岛县| 璧山县| 呼伦贝尔市| 宾阳县| 平泉县| 咸丰县| 遵化市| 古丈县| 荥阳市| 罗平县| 永德县| 平武县| 惠东县|