log4j应用教程Word下载.docx

上传人:b****5 文档编号:16155395 上传时间:2022-11-21 格式:DOCX 页数:15 大小:19.59KB
下载 相关 举报
log4j应用教程Word下载.docx_第1页
第1页 / 共15页
log4j应用教程Word下载.docx_第2页
第2页 / 共15页
log4j应用教程Word下载.docx_第3页
第3页 / 共15页
log4j应用教程Word下载.docx_第4页
第4页 / 共15页
log4j应用教程Word下载.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

log4j应用教程Word下载.docx

《log4j应用教程Word下载.docx》由会员分享,可在线阅读,更多相关《log4j应用教程Word下载.docx(15页珍藏版)》请在冰豆网上搜索。

log4j应用教程Word下载.docx

而且,通过使用各种第三方扩展,您可以很方便地将Log4j集成到J2EE、JINI甚至是SNMP应用中。

本文介绍的Log4j版本是1.2.3。

作者试图通过一个简单的客户/服务器Java程序例子对比使用与不使用Log4j1.2.3的差别,并详细讲解了在实践中最常使用Log4j的方法和步骤。

在强调可重用组件开发的今天,相信Log4j将会给广大的设计开发人员带来方便。

加入到Log4j的队伍来吧!

回页首

一个简单的例子

我们先来看一个简单的例子,它是一个用Java实现的客户/服务器网络程序。

刚开始我们不使用Log4j,而是使用了一系列的打印语句,然后我们将使用Log4j来实现它的日志功能。

这样,大家就可以清楚地比较出前后两个代码的差别。

2.1.不使用Log4j

2.1.1.客户程序

packagelog4j;

importjava.io.*;

import.*;

/**

*

*<

p>

ClientWithoutLog4j<

/p>

Description:

asamplewithlog4j<

*@version1.0

*/

publicclassClientWithoutLog4j{

/**

*@paramargs

publicstaticvoidmain(Stringargs[]){

Stringwelcome=null;

Stringresponse=null;

BufferedReaderreader=null;

PrintWriterwriter=null;

InputStreamin=null;

OutputStreamout=null;

Socketclient=null;

try{

client=newSocket("

localhost"

8001);

System.out.println("

info:

Clientsocket:

"

+client);

in=client.getInputStream();

out=client.getOutputStream();

}catch(IOExceptione){

error:

IOException:

+e);

System.exit(0);

}

try{

reader=newBufferedReader(newInputStreamReader(in));

writer=newPrintWriter(newOutputStreamWriter(out),true);

welcome=reader.readLine();

debug:

Serversays:

'

"

+welcome+"

'

);

HELLO"

writer.println("

HELLO"

response=reader.readLine();

Serverresponds:

+response+"

);

HELP"

HELP"

QUIT"

QUIT"

warn:

IOExceptioninclient.in.readln()"

System.out.println(e);

Thread.sleep(2000);

}catch(Exceptionignored){}

}

2.1.2.服务器程序

importjava.util.*;

ServerWithoutLog4j<

publicclassServerWithoutLog4j{

finalstaticintSERVER_PORT=8001;

//thisserver'

sport

StringclientRequest=null;

ServerSocketserver=null;

Socketsocket=null;

server=newServerSocket(SERVER_PORT);

ServerSocketbeforeaccept:

+server);

Javaserverwithoutlog4j,on-line!

//waitforclient'

sconnection

socket=server.accept();

ServerSocketafteraccept:

in=socket.getInputStream();

out=socket.getOutputStream();

System.out.println("

ServerconstructorIOException:

reader=newBufferedReader(newInputStreamReader(in));

writer=newPrintWriter(newOutputStreamWriter(out),true);

//sendwelcomestringtoclient

Javaserverwithoutlog4j,"

+newDate());

while(true){

//readfromclient

clientRequest=reader.readLine();

Clientsays:

+clientRequest);

if(clientRequest.startsWith("

)){

OK!

Vocabulary:

HELPQUIT"

else{

else{

Command'

+

clientRequest+"

notunderstood."

Command'

+clientRequest

+"

IOExceptioninServer"

2.2.迁移到Log4j

2.2.1.客户程序

//addforlog4j:

importsomepackage

importorg.apache.log4j.PropertyConfigurator;

importorg.apache.log4j.Logger;

importorg.apache.log4j.Level;

ClientWithLog4j<

asamplewithlog

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 小学教育 > 英语

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1