作者: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已经可以正常使用了。(未完待续)