春風(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 閱讀(2624) 評(píng)論(0)  編輯  收藏 所屬分類: Java基礎(chǔ)

          sitinspring(http://www.aygfsteel.com)原創(chuàng),轉(zhuǎn)載請(qǐng)注明出處.
          主站蜘蛛池模板: 洱源县| 彭州市| 新宾| 托里县| 沁源县| 萍乡市| 平阳县| 民勤县| 涟水县| 淄博市| 万源市| 屏南县| 确山县| 洪雅县| 玛曲县| 高密市| 会昌县| 苍山县| 光山县| 普兰店市| 九江县| 开原市| 滦南县| 宝坻区| 文水县| 大足县| 界首市| 贺兰县| 隆回县| 七台河市| 承德县| 墨竹工卡县| 元江| 建德市| 兰州市| 三都| 盐山县| 乐平市| 湘阴县| 大埔县| 佛冈县|