谢谢!~

解决方案 »

  1.   

    下载log4j,其中的jar包放到应用的lib目录下,同时在你的classes目录内新建一个log4j.properties文件,这个文件是用来配置日志输出到什么地方、以什么格式输出、输出哪些包内的类的日志文件等等。内容类似如下:# This is the configuring for logging displayed in the Application Server
    log4j.rootCategory=INFO, stdout# Replace the line above if you want to put a log file into the directory
    # you start Tomcat from
    # log4j.rootCategory=INFO, stdout, Rlog4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout# Pattern to output the caller's file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=[项目名] %p [%t] %c{1}.%M(%L) | %m%nlog4j.appender.R=org.apache.log4j.RollingFileAppender# You can change this to be an absolute path or even an environment variable
    # If you're using an environment variable, you will have to set JAVA_OPTS
    # to contain this variables - for example in the catalina.sh or catalina.bat
    # filelog4j.appender.R.File=日志文件名.loglog4j.appender.R.MaxFileSize=100KB# Don't keep a backup file
    log4j.appender.R.MaxBackupIndex=0log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n# Configuration for receiving e-mails when ERROR messages occur.
    log4j.appender.mail=org.apache.log4j.net.SMTPAppender
    log4j.appender.mail.To=用户邮箱
    log4j.appender.mail.From=localhost@公司邮箱.com
    log4j.appender.mail.SMTPHost=localhost
    log4j.appender.mail.Threshold=ERROR
    log4j.appender.mail.BufferSize=1
    log4j.appender.mail.Subject=[localhost] yxgs_egov Application Errorlog4j.appender.mail.layout=org.apache.log4j.PatternLayout
    log4j.appender.mail.layout.ConversionPattern=%d  %-5p %c %x - %m%n# If programmed properly the most messages would be at DEBUG
    # and the least at FATAL.
    log4j.logger.希望输入日志的包名=DEBUG
    # Control logging for other open source packages
    log4j.logger.com.opensymphony.oscache=ERROR
    log4j.logger.net.sf.navigator=ERROR
    log4j.logger.net.sf.acegisecurity=WARN
    log4j.logger.net.sf.acegisecurity.intercept.event.LoggerListener=WARN
    log4j.logger.org.apache.commons=ERROR
    log4j.logger.org.apache.struts=WARN
    log4j.logger.org.displaytag=ERROR
    log4j.logger.org.springframework=INFO
    log4j.logger.com.ibatis.db=WARN
    log4j.logger.org.apache.velocity=WARN# Don't show debug logs for WebTest
    log4j.logger.com.canoo.webtest=WARN# All hibernate log output of "info" level or higher goes to stdout.
    # For more verbose logging, change the "info" to "debug" on the last line.
    log4j.logger.org.hibernate.ps.PreparedStatementCache=WARN
    log4j.logger.org.hibernate=INFO# Changing the log level to DEBUG will result in Hibernate generated
    # SQL to be logged.
    log4j.logger.org.hibernate.SQL=ERROR# Changing the log level to DEBUG will result in the PreparedStatement
    # bound variable values to be logged.
    log4j.logger.org.hibernate.type=ERROR具体配置可以参看log4j提供的帮助文档。
    然后你就可以在程序中调用log4j的api来输出各种级别的日志信息。如果你使用eclipse开发环境,那么推荐使用log4e插件,它可以方便的帮你自动完成log4j代码的书写。^_^
      

  2.   


    #### Use two appenders, one to log to console, another to log to a file
    log4j.rootCategory=debug,stdout, R
    # Print only messages of priority WARN or higher for your category
    log4j.category.your.category.name=WARN
    #### First appender writes to console
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    # Pattern to output the caller's file name and line number.
    log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
    ########################
    # JDBC Appender
    #### Second appender writes to a file
    log4j.appender.R=org.apache.log4j.RollingFileAppender
    log4j.appender.R.File=daily.log
    # Control the maximum log file size
    log4j.appender.R.MaxFileSize=2048KB
    # Archive log files (one backup file here)
    log4j.appender.R.MaxBackupIndex=50
    log4j.appender.R.layout=org.apache.log4j.PatternLayout
    log4j.appender.R.layout.ConversionPattern=%-5r %-5p [%t] %c{2} %l %d - %m%n
      

  3.   

    我也是用LOG4J的很好用,这里有关于LOG4J的文章http://www.blogjava.net/yczz/articles/41900.html
      

  4.   

    同意,log4j,学习起来也不太难,但用起来很方便,而且还可以跟数据库相连用呢
      

  5.   

    log4j很不错的,用起来很上手。
      

  6.   

    package com.bobo.model;import java.io.IOException;
    import java.util.logging.FileHandler;
    import java.util.logging.Handler;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.util.logging.SimpleFormatter;public class Log { public void useLog() {
    try {
    Logger.getLogger("").setLevel(Level.ALL);
    Handler handler = new FileHandler("bobo.log", true);
    handler.setFormatter(new SimpleFormatter());
    Logger.getLogger("").addHandler(handler);
    } catch (SecurityException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }package test.bobo.util;import java.io.File;
    import java.util.logging.Logger;import junit.framework.TestCase;import com.bobo.model.Log;public class LogTest extends TestCase { protected void setUp() throws Exception {
    super.setUp();
    File file = new File("bobo.log");
    if (file.exists()) {
    file.delete();
    }
    } public void testLog() {
    File file = new File("bobo.log");
    assertEquals(false, file.exists());
    Log log = new Log();
    log.useLog();
    assertEquals(true, file.exists());
    assertEquals(0, file.length());
    Logger logger = Logger.getLogger("");
    logger.fine("0123456789");
    }
    }给你个简单的例子吧,虽然Java本身的日志系统有些麻烦,但是了解一下还是好的。
    我也只是没事的时候看了看,但是上面的代码可以作为一个简单的日志了,运行主函数先调用useLog() ,然后在其它部分就可以直接使用了。
      

  7.   

    使用log4j,配置文件有两种形式。
    一种是properties文件;
    一种是xml文件。
      

  8.   

    通常使用properties文件来初始化log4j,可是这种方式不方便动态的对log4j的配置内容进行修改,使用xml文件就方便多了,下面是一个log4j配置文件的简单例子。
    <!-- log4j.xml -->
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <!-- appender 部分是可用的日志输出方式定义,可以定义多个 -->
            <appender name="CONSOLE_LOG"  class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%-5p  %d %-15c{1} [%x]: %m%n" />
    </layout>
             </appender>
    <!--  root部分定义了log4j的默认输出级别和方式 -->
             <root>
    <priority value="fatal" />
    <appender-ref ref="CONSOLE_LOG" />
            </root>
    <!--  category 部分定义了特定包或者类的输出级别和方式,可以有多个 -->
           <category name="org.apache">
               <priority value="warn"/>
               <appender-ref ref="CONSOLE_LOG"/>
            </category>        <category name="net.sf">
               <priority value="warn"/>
               <appender-ref ref="CONSOLE_LOG"/>
            </category></log4j:configuration>