欢迎光临
我们一直在努力

JBuilder2005+JBoss-4.0.2RC1+J2SDK1.5+Log4j 开发Session Bean例解(2)-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

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

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » JBuilder2005+JBoss-4.0.2RC1+J2SDK1.5+Log4j 开发Session Bean例解(2)-JSP教程,Java技巧及代码
分享到: 更多 (0)