在这里查了一下,很多都是用 log4j,它是如何的一个工作原理呢,我应该从何下手呢?

解决方案 »

  1.   

    用log4j
    PropertyConfigurator.config("C:\log4j.properties");
    Logger log=Logger.getLogger(A.class);
    然后配置下log4j.properties这个文件 这个文件是设置你输出到哪里和级别的
    log4j.rootLogger=DEBUG,stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    提供个比较简单的给你 至于工作原理 上网查 一下
      

  2.   

    找些资料吧,不是一句两句能说清的
    http://www.javaresearch.org/article/59840.htm
      

  3.   

    dlxu(Coding超过了15W行) ( ) 信誉:114    Blog  2006-11-30 23:17:08  得分: 0  
     
     
       
    1 下载log4j的包,把jar文件放在你的classpath下
    2 配置log4j.properties或者log4j.xml,放在classpath下
    3 在程序中使用log4j来出log吧  
     
      

  4.   

    public class ServerWithLog4j { 
         final static int SERVER_PORT = 8001 ; // this server's port    /*
        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 final Logger logger = (Logger) Logger.getInstance(ServerWithLog4j.class);    /**
         * main method
         * @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 ;        /*
            add for log4j: class BasicConfigurator can quickly configure the package.
            print the information to console.
            */
            PropertyConfigurator.configure ( "ServerWithLog4j.properties" ) ;        // add for log4j: set the level
            logger.setLevel ( ( Level ) Level.DEBUG ) ;        try{
                server = new ServerSocket ( SERVER_PORT ) ;
                // add for log4j: log a message with the info level
                logger.info ( "ServerSocket before accept: " + server ) ;
                // add for log4j: log a message with the info level
                logger.info ( "Java server with log4j, on-line!" ) ;
                // wait for client's connection
                socket = server.accept() ;
                // add for log4j: log a message with the info level
                logger.info ( "ServerSocket after accept: " + server ) ;            in = socket.getInputStream() ;
                out = socket.getOutputStream() ;
            } catch ( IOException e ) {
                // add for log4j: log a message with the error level
                logger.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 with log4j, " + new Date () ) ;        while ( true ) {
                try {
                    // read from client
                    clientRequest = reader.readLine () ;
                    // add for log4j: log a message with the debug level
                    logger.debug ( "Client says: " + clientRequest ) ;
                    if ( clientRequest.startsWith ( "HELP" ) ) {
                        // add for log4j: log a message with the debug level
                        logger.debug ( "OK!" ) ;
                        writer.println ( "Vocabulary: HELP QUIT" ) ;
                    }
                    else {
                        if ( clientRequest.startsWith ( "QUIT" ) ) {
                            // add for log4j: log a message with the debug level
                            logger.debug ( "OK!" ) ;
                            System.exit ( 0 ) ;
                        }
                        else {
                            // add for log4j: log a message with the warn level
                            logger.warn ( "Command '" + clientRequest + "' not understood." ) ;
                            writer.println ( "Command '" + clientRequest + "' not understood." ) ;
                        }
                    }
                } catch ( IOException e ) {
                    // add for log4j: log a message with the error level
                    logger.error( "IOException in Server " + e ) ;
                    System.exit ( 0 ) ;
                }
            }
        }
    }
      

  5.   

    用log4j写日志,为什么我写的这一句:PropertyConfigurator.config("D:\\project\\mytree\\WebRoot\\log4j.properties");
    它总在报错啊! 详细代码如下:
    package myexcel;import jxl.*;
    import jxl.format.UnderlineStyle;
    import jxl.write.*;
    import jxl.write.Number;
    import jxl.write.Boolean;
    import java.io.*;
    import java.util.*;
    import myBean.*;
    import java.io.File;
    import org.apache.log4j.Level;
    import org.apache.log4j.Logger;
    import org.apache.log4j.PatternLayout;
    import org.apache.log4j.RollingFileAppender;
    public class MyExcel {
    /**
         * 读取Excel
         * @param filePath
         */
    static final Logger logger = (Logger) Logger.getLogger(MyExcel.class);

        public ArrayList readExcel(String filePath)
        {
         ArrayList _myExA=new ArrayList();
         PropertyConfigurator.config("D:\\project\\mytree\\WebRoot\\log4j.properties");
      

  6.   

    加这句话:import org.apache.log4j.*;
      

  7.   

    在日志文件中,logger可以设为多个,形成继承的关系。我不明白的是,要多个logger起什么作用啊!
      

  8.   

    我对日志的理解:log4j是一个类(需要下载下载log4j的包、在类中引入),这个类实现功能:实例化一个日志:static final Logger logger = (Logger) Logger.getLogger(类的名字.class);调用这个类的方法可以把字符信息输出给不同的地方(地方可以用appender方法指定)。但它也是有一定规则来定义输出的格式,可用几种方法实现:
    通过程序配置 
    通过Property文件配置  :PropertyConfigurator.configure ( "配置文件名字.properties" ) ;
    通过XML文件配置设置输出级别:logger.setLevel ( ( Level ) Level.DEBUG ) ; 写需要输出的信息:    logger.info ( "ServerSocket before accept: " + server ) ;
      

  9.   

    对于日志log4j可以在网上搜到很多技术文章:下面是几个地址,可以看看:
    http://zooo.51.net/heavyz_cs/notebook/log4j.html
    http://www.javaresearch.org/article/59840.htm
    http://www.solol.org/technologic/java/j-log4j/#resource