作者:Junsan Jin
日期:2005-3-30
版本:1.0
信箱:[email protected] ; [email protected]
聲明:本人保留本文的所有權利。
第二部分:配置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,在
OK,至此我們的Log4j已經可以正常使用了。(未完待續)