春風博客

          春天里,百花香...

          導航

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

          統計

          公告

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

          Locations of visitors to this page

          常用鏈接

          留言簿(11)

          隨筆分類(224)

          隨筆檔案(126)

          個人軟件下載

          我的其它博客

          我的鄰居們

          最新隨筆

          搜索

          積分與排名

          最新評論

          閱讀排行榜

          評論排行榜

          Tomcat工程中的log4j配置

          1)log4j.properties文件內容如下,此文件要配置到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)書寫一個初始化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");//設置路徑
                       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>

          此后配置文件就出現在工程目錄下。

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

          sitinspring(http://www.aygfsteel.com)原創,轉載請注明出處.
          主站蜘蛛池模板: 岫岩| 苍山县| 崇义县| 关岭| 基隆市| 甘谷县| 常熟市| 随州市| 嘉峪关市| 台南市| 南通市| 乌拉特后旗| 贺州市| 措美县| 疏勒县| 苗栗市| 铜川市| 广元市| 贵州省| 阳城县| 盈江县| 丹巴县| 姜堰市| 屯门区| 老河口市| 浠水县| 邢台市| 安岳县| 乾安县| 洛隆县| 宝清县| 郴州市| 木兰县| 巨野县| 阿鲁科尔沁旗| 江安县| 惠水县| 张家川| 苏尼特右旗| 永城市| 济宁市|