概述 背景 在应用程序中添加日志记录总的来说基于三个目的监视代码中变量的变化情况周期性的记录到文件中供其他应用进行统计分析工作跟蹤代码运行时轨迹作为日后审计的依据担当集成开发环境中的调试器的作用向文件或控制台打印代码的调试信息 最普通的做法就是在代码中嵌入许多的打印语句这些打印语句可以输出到控制台或文件中比较好的做法就是构造一个日志操作类来封装此类操作而不是让一系列的打印语句充斥了代码的主体 Logj简介 在强调可重用组件开发的今天除了自己从头到尾开发一个可重用的日志操作类外Apache为我们提供了一个强有力的日志操作包Logj Logj是Apache的一个开放源代码项目通过使用Logj我们可以控制日志信息输送的目的地是控制台文件GUI组件甚至是套接口服务器NT的事件记录器UNIX Syslog守护进程等我们也可以控制每一条日志的输出格式通过定义每一条日志信息的级别我们能够更加细致地控制日志的生成过程最令人感兴趣的就是这些可以通过一个配置文件来灵活地进行配置而不需要修改应用的代码 此外通过Logj其他语言接口您可以在CC++NetPL/SQL程序中使用Logj其语法和用法与在Java程序中一样使得多语言分布式系统得到一个统一一致的日志组件模块而且通过使用各种第三方扩展您可以很方便地将Logj集成到JEEJINI甚至是SNMP应用中 本文介绍的Logj版本是作者试图通过一个简单的客户/服务器Java程序例子对比使用与不使用Logj 的差别并详细讲解了在实践中最常使用Logj的方法和步骤在强调可重用组件开发的今天相信Logj将会给广大的设计开发人员带来方便加入到Logj的队伍来吧! 一个简单的例子 我们先来看一个简单的例子它是一个用Java实现的客户/服务器网络程序刚开始我们不使用Logj而是使用了一系列的打印语句然后我们将使用Logj来实现它的日志功能这样大家就可以清楚地比较出前后两个代码的差别 不使用Logj 客户程序 package logj ; import javaio* ; import * ; /** * * <p> Client Without Logj </p> * <p> Description: a sample with logj</p> * @version */ public class ClientWithoutLogj { /** * * @param args */ public static void main ( String args [] ) { String welcome = null; String response = null; BufferedReader reader = null; PrintWriter writer = null; InputStream in = null; OutputStream out = null; Socket client = null; try { client = new Socket ( localhost ) ; Systemoutprintln ( info: Client socket: + client ) ; in = clientgetInputStream () ; out = clientgetOutputStream () ; } catch ( IOException e ) { Systemoutprintln ( error: IOException : + e ) ; Systemexit ( ) ; } try{ reader = new BufferedReader( new InputStreamReader ( in ) ) ; writer = new PrintWriter ( new OutputStreamWriter ( out ) true ) ; welcome = readerreadLine () ; Systemoutprintln ( debug: Server says: + welcome + ) ; Systemoutprintln ( debug: HELLO ) ; writerprintln ( HELLO ) ; response = readerreadLine () ; Systemoutprintln ( debug: Server responds: + response + ) ; Systemoutprintln ( debug: HELP ) ; writerprintln ( HELP ) ; response = readerreadLine () ; Systemoutprintln ( debug: Server responds: + response + ) ; Systemoutprintln ( debug: QUIT ) ; writerprintln ( QUIT ) ; } catch ( IOException e ) { Systemoutprintln ( warn: IOException in clientinreadln() ) ; Systemoutprintln ( e ) ; } try{ Threadsleep ( ) ; } catch ( Exception ignored ) {} } } 服务器程序 package logj ; import javautil* ; import javaio* ; import * ; /** * * <p> Server Without Logj </p> * <p> Description: a sample with logj</p> * @version */ public class ServerWithoutLogj { final static int SERVER_PORT = ; // this servers port /** * * @param args */ public static void main ( String args [] ) { String clientRequest = null; BufferedReader reader = null; PrintWriter writer = null; ServerSocket server = null; Socket socket = null; InputStream in = null; OutputStream out = null; try { server = new ServerSocket ( SERVER_PORT ) ; Systemoutprintln ( info: ServerSocket before accept: + server ) ; Systemoutprintln ( info: Java server without logj online! ) ; // wait for clients connection socket = serveraccept () ; Systemoutprintln ( info: ServerSocket after accept: + server ); in = socketgetInputStream () ; out = socketgetOutputStream () ; } catch ( IOException e ) { Systemoutprintln( error: Server constructor IOException: + e ) ; Systemexit ( ) ; } reader = new BufferedReader ( new InputStreamReader ( in ) ) ; writer = new PrintWriter ( new OutputStreamWriter ( out ) true ) ; // send welcome string to client writerprintln ( Java server without logj + new Date () ) ; while ( true ) { try { // read from client clientRequest = readerreadLine () ; Systemoutprintln ( debug: Client says: + clientRequest ) ; if ( clientRequeststartsWith ( HELP ) ) { Systemoutprintln ( debug: OK! ) ; writerprintln ( Vocabulary: HELP QUIT ) ; } else { if ( clientRequeststartsWith ( QUIT ) ) { Systemoutprintln ( debug: OK! ) ; Systemexit ( ) ; } else{ Systemoutprintln ( warn: Command + clientRequest + not understood ) ; writerprintln ( Command + clientRequest + not understood ) ; } } } catch ( IOException e ) { Systemoutprintln ( error: IOException in Server + e ) ; Systemexit ( ) ; } } } } 迁移到Logj 客户程序 package logj ; import javaio* ; import * ; // add for logj: import some package import orgapachelogjPropertyConfigurator ; import orgapachelogjLogger ; import orgapachelogjLevel ; /** * * <p> Client With Logj </p> * <p> Description: a sample with logj< |