⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log4j_usage_detail.txt

📁 log4j使用文档中的中上品。 包括:1
💻 TXT
📖 第 1 页 / 共 3 页
字号:
            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. 服务器程序 


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 ) {}
    }
}

 

2.2.2. 服务器程序 


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -