JBuilder2005+JBoss-4.0.2RC1+J2SDK1.5+Log4j 開發Session Bean例解(2)

來源:互聯網
上載者:User
session
作者:Junsan Jin

日期:2005-3-30

版本:1.0

信箱:junsan21@126.com ; junnef21@sohu.com

聲明:本人保留本文的所有權利。





第二部分:配置Log4j







Log4j主要是針對java的記錄檔產生管理組件,這裡使用logging-log4j-1.2.9。解壓logging-log4j-1.2.9的壓縮包,任意路徑即可,這裡是D:\logging-log4j-1.2.9。







先在JBuilder中建立工程,然後引入log4j庫檔案。 



建立工程:







選擇菜單:new project…,name為Trader,點擊finish。







這裡工程目錄為:E:\projects\Trader。 



拷貝類庫:







在trader檔案夾下建立lib檔案夾,拷貝D:\logging-log4j-1.2.9\dist\lib\log4j-1.2.9.jar到E:\projects\Trader\lib下。 



將類庫引入工程:







選擇菜單:project〉project properties 〉Paths 〉右邊Tab頁Required Libraries > Add > Tab頁Archives,選擇E:\projects\Trader\lib,右邊的列表框中選擇log4j-1.2.9.jar,一路ok即可。 



建立web模組:







選擇菜單:New 〉 Web 〉Web module(WAR)〉 OK按鈕 〉Create Empty Web Module 〉 Next按鈕,填入名稱WebTrader,點擊Finish按鈕。 



增加properties設定檔:







在JBuilder左邊的菜單樹上選擇WebTrader 〉Module Directory 〉選中WEB-INF 〉右鍵new 〉File,填入log4j.properties確定即可。







編輯log4j.properties的內容如下:







-------------------------------------------------







log4j.properties







--------------------------------------------------







# For the general syntax of property based configuration files see the







# documenation of org.apache.log4j.PropertyConfigurator. 



# The root category uses the appender called A1. Since no priority is







# specified, the root category assumes the default priority for root







# which is DEBUG in log4j. The root category is the only category that







# has a default priority. All other categories need not be assigned a







# priority in which case they inherit their priority from the







# hierarchy. 



log4j.rootCategory=, A1 



# A1 is set to be a FileAppender which outputs to the file







# "factor.log". Start the server NumberCruncherServer and two







# NumberCruncherClients, and ask to factor two numbers







# near-simultaneously. Notice that the log output from these two







# requests are logged in the file factor.log. Nevertheless, the logs







# of these requests can still be distinguished given their distinct







# nested diagnostic contexts. 



log4j.appender.A1=org.apache.log4j.FileAppender







log4j.appender.A1.File=${jboss.server.home.dir}/log/trader.log







log4j.appender.A1.layout=org.apache.log4j.PatternLayout 



# Note the %x conversion specifier for NDC printing.







# log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] (%x) - %m\n







log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n







Log4j.appender.A1.MaxFileSize=100KB 



增加log4j環境初始化類:







右鍵點擊菜單樹中Trader.jpx 〉 New 〉Package,填入com.inspiresky.trader.init,確定即可。







右鍵點擊建立的init包〉new 〉class,名字為Log4jInit.java,確定所在包為com.inspiresky.trader.init,點擊OK鍵即可。







編輯Log4jInit.java的內容如下:







-----------------------------------------------------







Log4jInit.java







-----------------------------------------------------







package com.inspiresky.trader.init; 



import org.apache.log4j.PropertyConfigurator;







import javax.servlet.http.HttpServlet;







import javax.servlet.http.HttpServletRequest;







import javax.servlet.http.HttpServletResponse;







import java.io.PrintWriter;







import java.io.IOException;







import javax.servlet.ServletException; 



public class Log4jInit extends HttpServlet { 



    //Initialize global variables







    public void init() throws ServletException {







        String prefix =  getServletContext().getRealPath("/");







        String file = getInitParameter("log4j-init-file");







        // if the log4j-init-file is not set, then no point in trying







        if(file != null) {







            PropertyConfigurator.configure(prefix+file);







        }







    } 



    public void doGet(HttpServletRequest req, HttpServletResponse res) {







    } 



    //Clean up resources







    public void destroy() {







    }











配置web.xml檔案:







在JBuilder左邊的菜單樹上選擇WebTrader 〉Module Directory 〉WEB-INF 下,開啟web.xml,在<display-name>WebTrader</display-name>下增加如下內容:







<servlet>







    <servlet-name>log4j-init</servlet-name>







    <servlet-class>com.inspiresky.trader.init.Log4jInit</servlet-class>







    <init-param>







      <param-name>log4j-init-file</param-name>







      <param-value>WEB-INF/log4j.properties</param-value>







    </init-param>







    <load-on-startup>1</load-on-startup>







  </servlet> 



OK,至此我們的Log4j已經可以正常使用了。(未完待續)


相關文章

Beyond APAC's No.1 Cloud

19.6% IaaS Market Share in Asia Pacific - Gartner IT Service report, 2018

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。