<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    【永恒的瞬間】
    ?Give me hapy ?
    ?

    概述

    1.1. 背景

    在應(yīng)用程序中添加日志記錄總的來說基于三個(gè)目的:監(jiān)視代碼中變量的變化情況,周期性的記錄到文件中供其他應(yīng)用進(jìn)行統(tǒng)計(jì)分析工作;跟蹤代碼運(yùn)行時(shí)軌跡,作為日后審計(jì)的依據(jù);擔(dān)當(dāng)集成開發(fā)環(huán)境中的調(diào)試器的作用,向文件或控制臺(tái)打印代碼的調(diào)試信息。

    最普通的做法就是在代碼中嵌入許多的打印語句,這些打印語句可以輸出到控制臺(tái)或文件中,比較好的做法就是構(gòu)造一個(gè)日志操作類來封裝此類操作,而不是讓一系列的打印語句充斥了代碼的主體。

    1.2. Log4j簡(jiǎn)介

    在強(qiáng)調(diào)可重用組件開發(fā)的今天,除了自己從頭到尾開發(fā)一個(gè)可重用的日志操作類外,Apache為我們提供了一個(gè)強(qiáng)有力的日志操作包-Log4j。

    Log4j是Apache的一個(gè)開放源代碼項(xiàng)目,通過使用Log4j,我們可以控制日志信息輸送的目的地是控制臺(tái)、文件、GUI組件、甚至是套接口服務(wù)器、NT的事件記錄器、UNIX Syslog守護(hù)進(jìn)程等;我們也可以控制每一條日志的輸出格式;通過定義每一條日志信息的級(jí)別,我們能夠更加細(xì)致地控制日志的生成過程。最令人感興趣的就是,這些可以通過一個(gè)配置文件來靈活地進(jìn)行配置,而不需要修改應(yīng)用的代碼。

    此外,通過Log4j其他語言接口,您可以在C、C++、.Net、PL/SQL程序中使用Log4j,其語法和用法與在Java程序中一樣,使得多語言分布式系統(tǒng)得到一個(gè)統(tǒng)一一致的日志組件模塊。而且,通過使用各種第三方擴(kuò)展,您可以很方便地將Log4j集成到J2EE、JINI甚至是SNMP應(yīng)用中。

    本文介紹的Log4j版本是1.2.3。作者試圖通過一個(gè)簡(jiǎn)單的客戶/服務(wù)器Java程序例子對(duì)比使用與不使用Log4j 1.2.3的差別,并詳細(xì)講解了在實(shí)踐中最常使用Log4j的方法和步驟。在強(qiáng)調(diào)可重用組件開發(fā)的今天,相信Log4j將會(huì)給廣大的設(shè)計(jì)開發(fā)人員帶來方便。加入到Log4j的隊(duì)伍來吧!





    回頁首


    一個(gè)簡(jiǎn)單的例子

    我們先來看一個(gè)簡(jiǎn)單的例子,它是一個(gè)用Java實(shí)現(xiàn)的客戶/服務(wù)器網(wǎng)絡(luò)程序。剛開始我們不使用Log4j,而是使用了一系列的打印語句,然后我們將使用Log4j來實(shí)現(xiàn)它的日志功能。這樣,大家就可以清楚地比較出前后兩個(gè)代碼的差別。

    2.1. 不使用Log4j

    2.1.1. 客戶程序

    												
    														package log4j ;
    
    import java.io.* ;
    import java.net.* ;
    
    /**
     *
     * <p> Client Without Log4j </p>
     * <p> Description: a sample with log4j</p>
     * @version 1.0
     */
    public class ClientWithoutLog4j {
    
        /**
         *
         * @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", 8001 ) ;
                System.out.println ( "info: Client socket: " + client ) ;
                in = client.getInputStream () ;
                out = client.getOutputStream () ;
            } catch ( IOException e ) {
                System.out.println ( "error: IOException : " + e ) ;
                System.exit ( 0 ) ;
            }
    
            try{
                reader = new BufferedReader( new InputStreamReader ( in ) ) ;
                writer = new PrintWriter ( new OutputStreamWriter ( out ), true ) ;
    
                welcome = reader.readLine () ;
                System.out.println ( "debug: Server says: '" + welcome + "'" ) ;
    
                System.out.println ( "debug: HELLO" ) ;
                writer.println ( "HELLO" ) ;
                response = reader.readLine () ;
                System.out.println ( "debug: Server responds: '" + response + "'") ;
    
                System.out.println ( "debug: HELP" ) ;
                writer.println ( "HELP" ) ;
                response = reader.readLine () ;
                System.out.println ( "debug: Server responds: '" + response + "'" ) ;
    
                System.out.println ( "debug: QUIT" ) ;
                writer.println ( "QUIT" ) ;
            } catch ( IOException e ) {
                System.out.println ( "warn: IOException in client.in.readln()" ) ;
                System.out.println ( e ) ;
            }
            try{
                Thread.sleep ( 2000 ) ;
            } catch ( Exception ignored ) {}
        }
    }
    
    												
    										

    2.1.2. 服務(wù)器程序

    												
    														package log4j ;
    
    import java.util.* ;
    import java.io.* ;
    import java.net.* ;
    
    /**
     *
     * <p> Server Without Log4j </p>
     * <p> Description: a sample with log4j</p>
     * @version 1.0
     */
    public class ServerWithoutLog4j {
    
        final static int SERVER_PORT = 8001 ; // this server's 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 ) ;
                System.out.println ( "info: ServerSocket before accept: " + server ) ;
                System.out.println ( "info: Java server without log4j, on-line!" ) ;
    
                // wait for client's connection
                socket = server.accept () ;
                System.out.println ( "info: ServerSocket after accept: " + server )  ;
    
                in = socket.getInputStream () ;
                out = socket.getOutputStream () ;
    
            } catch ( IOException e ) {
                System.out.println( "error: Server constructor IOException: " + e ) ;
                System.exit ( 0 ) ;
            }
            reader = new BufferedReader ( new InputStreamReader ( in ) ) ;
            writer = new PrintWriter ( new OutputStreamWriter ( out ) , true ) ;
    
            // send welcome string to client
            writer.println ( "Java server without log4j, " + new Date () ) ;
    
            while ( true ) {
                try {
                    // read from client
                    clientRequest = reader.readLine () ;
                    System.out.println ( "debug: Client says: " + clientRequest ) ;
                    if ( clientRequest.startsWith ( "HELP" ) ) {
                        System.out.println ( "debug: OK!" ) ;
                        writer.println ( "Vocabulary: HELP QUIT" ) ;
                    }
                    else {
                        if ( clientRequest.startsWith ( "QUIT" ) ) {
                            System.out.println ( "debug: OK!" ) ;
                            System.exit ( 0 ) ;
                        }
                        else{
                            System.out.println ( "warn: Command '" + 
    						clientRequest + "' not understood." ) ;
                            writer.println ( "Command '" + clientRequest 
    						+ "' not understood." ) ;
                        }
                    }
                } catch ( IOException e ) {
                    System.out.println ( "error: IOException in Server " + e ) ;
                    System.exit ( 0 ) ;
                }
            }
        }
    }
    
    												
    										

    2.2. 遷移到Log4j

    2.2.1. 客戶程序

    												
    														package log4j ;
    
    import java.io.* ;
    import java.net.* ;
    
    // add for log4j: import some package
    import org.apache.log4j.PropertyConfigurator ;
    import org.apache.log4j.Logger ;
    import org.apache.log4j.Level ;
    
    /**
     *
     * <p> Client With Log4j </p>
     * <p> Description: a sample with log4j</p>
     * @version 1.0
     */
    public class ClientWithLog4j {
    
        /*
        add for log4j: class Logger is the central class in the log4j package.
        we can do most logging operations by Logger except configuration.
        getLogger(...): retrieve a logger by name, if not then create for it.
        */
        static Logger logger = Logger.getLogger 
    	( ClientWithLog4j.class.getName () ) ;
    
        /**
         *
         * @param args : configuration file name
         */
        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 ;
    
            /*
            add for log4j: class BasicConfigurator can quickly configure the package.
            print the information to console.
            */
            PropertyConfigurator.configure ( "ClientWithLog4j.properties" ) ;
    
            // add for log4j: set the level
    //        logger.setLevel ( ( Level ) Level.DEBUG ) ;
    
            try{
                client = new Socket( "localhost" , 8001 ) ;
    
                // add for log4j: log a message with the info level
                logger.info ( "Client socket: " + client ) ;
    
                in = client.getInputStream () ;
                out = client.getOutputStream () ;
            } catch ( IOException e ) {
    
                // add for log4j: log a message with the error level
                logger.error ( "IOException : " + e ) ;
    
                System.exit ( 0 ) ;
            }
    
            try{
                reader = new BufferedReader ( new InputStreamReader ( in ) ) ;
                writer = new PrintWriter ( new OutputStreamWriter ( out ), true ) ;
    
                welcome = reader.readLine () ;
    
                // add for log4j: log a message with the debug level
                logger.debug ( "Server says: '" + welcome + "'" ) ;
    
                // add for log4j: log a message with the debug level
                logger.debug ( "HELLO" ) ;
    
                writer.println ( "HELLO" ) ;
                response = reader.readLine () ;
    
                // add for log4j: log a message with the debug level
                logger.debug ( "Server responds: '" + response + "'" ) ;
    
                // add for log4j: log a message with the debug level
                logger.debug ( "HELP" ) ;
    
                writer.println ( "HELP" ) ;
                response = reader.readLine () ;
    
                // add for log4j: log a message with the debug level
                logger.debug ( "Server responds: '" + response + "'") ;
    
                // add for log4j: log a message with the debug level
                logger.debug ( "QUIT" ) ;
    
                writer.println ( "QUIT" ) ;
            } catch ( IOException e ) {
    
                // add for log4j: log a message with the warn level
                logger.warn ( "IOException in client.in.readln()" ) ;
    
                System.out.println ( e ) ;
            }
            try {
                Thread.sleep ( 2000 ) ;
            } catch ( Exception ignored ) {}
        }
    }
    
    												
    										

    posted on 2007-01-03 16:36 ???MengChuChen 閱讀(281) 評(píng)論(0)  編輯  收藏 所屬分類: Log4j
    主站蜘蛛池模板: 国产亚洲?V无码?V男人的天堂| 高清免费久久午夜精品| 亚洲欭美日韩颜射在线二| a毛片基地免费全部视频| 在线观看特色大片免费网站| 亚洲AV成人无码久久WWW| 亚洲六月丁香六月婷婷蜜芽| 国产成A人亚洲精V品无码性色| 免费在线观看理论片| 最近中文字幕mv免费高清视频7 | 久久九九全国免费| 国产av无码专区亚洲av毛片搜| 亚洲永久在线观看| 亚洲福利电影在线观看| 久久亚洲国产视频| 亚洲乱码无码永久不卡在线| 亚洲国产综合精品中文字幕 | 久久久久亚洲av无码专区导航 | 国产美女在线精品免费观看| 亚洲av无码一区二区三区不卡| 午夜亚洲国产成人不卡在线| 美女被免费视频网站a国产| 亚洲成人免费网址| 免费精品一区二区三区第35| 中文字幕不卡免费高清视频| 中文字幕a∨在线乱码免费看 | 岛国大片免费在线观看| 国拍在线精品视频免费观看| 四虎在线最新永久免费| 97av免费视频| 91免费人成网站在线观看18| 亚洲免费电影网站| 成人免费观看一区二区| 免费精品国产自产拍在| 无人在线直播免费观看| 91在线视频免费看| 毛片a级毛片免费观看免下载| 久久精品无码一区二区三区免费| 中国在线观看免费国语版| 黄色成人免费网站| 免费精品人在线二线三线区别|