我写的如下代码时;
public class DialogPanel extends JPanel
{
static final Logger LOG = Logger.getLogger(DialogPanel.class.getName());
public DialogPanel()
{
super();
if(LOG.isEnabledFor(Level.DEBUG))
{
LOG.debug("Begin DialogPanel() constructor");
}
          }
}
会有错误提示:log4j:WARN No appenders could be found for logger (DialogPanel).
log4j:WARN Please initialize the log4j system properly.是不是要设置什么东西啊?

解决方案 »

  1.   

    编写一个随处可调用的静态日志操作类日志文件是一个随处都在使用的文件,它可以很好的记录程序的运行状态和出错信息,几乎每一个安装程序都有它的安装文件..在我们用java开发web项目中,会经常需要记录用户的一些登陆,访问,操作信息.如:一个办公系统,它的日志文件需要记录:
    ………
    Wed May 19 13:35:50 CST 2004:张三登陆了该系统
    Wed May 19 13:35:53 CST 2004:张三插入了一条"下周计划"记录 id:1048
    Wed May 19 13:35:54 CST 2004:李四登陆了该系统
    Wed May 19 13:35:55 CST 2004:张三删除了一条"上周总结"记录, d:1024
    Wed May 19 13:35:59 CST 2004:张三退出了该系统
    ................
    实现思路:
    1. 为了很好的实现这个记录类,必须要使用”单态”模式,这样该类不必每次调用的时候都需要生成它的实例,初始化i/o.(这在一定程度上是很耗费时间的).
    2. 为了防止多线程同时操作(写)日志文件,造成文件”死锁”,必须考虑同步,使用synchronized关键字.
    3. 为了不必关心该类唯一实例的生成,而直接使用该类的静态方法实现日志的记录
    4. 为了更方便的配置日志文件的路径,使用属性文件配置.废话太多了,不像搞程序的了,直接看代码,所有的注释在代码中说明:
    import java.io.*;
    import java.util.*;
    public class LogWriter {
    private static final String DefalutLogFilePathName="c:\\logtext.log";//默认的日志文件的路径和文件名称
    private static LogWriter logwriter; //该类的唯一的实例
    private static InputStream fin; //属性配置文件的输入流
    private static Properties pro; //class Properties´s supper is Hashtable class
    private static PrintWriter out; //output stream
    private static String logFileName; //output file name
    private LogWriter() {
    outInit();//init out put stream,实例化PrintWriter out 对象.
    }
    /**保存你想保存在日志文件中的信息,实现同步
    * out put the message infomation
    * @param message infomation
    */
    public static synchronized void log(String message) {
    if (logwriter == null || (out == null)){
    logwriter = new LogWriter();
    }
    if (out != null) {
    out.println(new java.util.Date() + ":" + message);
    }
    }
    /**把异常信息保存在日志文件中,实现同步
    * out put the Excetion infomation
    * @param message infomation
    */
    public static synchronized void log(Exception ex) {
    if (logwriter == null || (out == null))
    logwriter = new LogWriter();
    if (out != null) {
    out.println(new java.util.Date() + ":" );
    ex.printStackTrace(out);
    }
    }
    /**
    *输出文件流的init
    */
    private void outInit() {
    if (logFileName == null)
    logFileName = getlogFileName(); //从属性文件中类获得日志文件的路径
    try {
    if (out == null) {//如果输出i/o没有实例,则生成一个信的
    out = new PrintWriter(new FileWriter(logFileName, true), true); ; //
    //其中的FileWriter()中的第二个参数的含义是:是否在文件中追加内容
    }
    }
    catch (IOException ex) {
    System.out.println("无法打开日志文件:"+logFileName);
    ex.printStackTrace();
    out = null;
    }
    }
    /**
    *根据配置文件.来获得日志文件的位置
    *
    * @return logFileName
    */
    private String getlogFileName() {
    try {
    if (pro == null) {
    pro = new java.util.Properties();
    fin = getClass().getResourceAsStream("log.properties"); //在类的当前位置,查找属性配置文件log.properties
    pro.load(fin);//载入配置文件
    fin.close();
    }
    }
    catch (IOException ex) {
    System.err.println("无法打开属性配置文件: log.properties" );
    ex.printStackTrace();
    }
    return pro.getProperty("logfile",DefalutLogFilePathName);
    //根据属性值获得日志文件路径,第二个参数是:如果找不到"logfile"标志,就返回的默认值
    }
    /**你也可以在所有的日志都记录完成的时候,调用该方法,释放资源.
    * free all the resouce,this is secuty method
    */
    public void free() {
    try {
    this.logwriter = null;
    if (out != null)
    this.out.close();
    if (fin != null)
    this.fin.close();
    }
    catch (IOException ex) {
    ex.printStackTrace();
    }
    }
    }
    ●类的具体使用::
    1.,把该类编译好, 新建立属性配置文件:log.properties,并确保把它放倒你的这个编译好的类所在的位置
    文件内容如下:当然你可以把路径修改为你想要的路径
    logfile=e:\\logtext.log
    2.. 使用举例:
    使用1:
    LogWriter.log("张三登陆了该系统");
    logWriter.log("张三删除了xxx条记录:记录id:");
    使用2:
    try{
    }
    catch (Exception ex) {
    LogWriter.log(ex);
    }
    ● 几点说明:
    一.其中的 getClass().getResourceAsStream("文件名称")不支持static的调用,
    所以要把该类换为非static,但是它的调用仅仅在于outinit()中调用,而outinit()
    也仅仅在私有的构造函数中调用,而私有构造函数可以在静态的static 中被调用,
    这样就达到了可以利用静态方法来调用随时输入日志,并保证了仅仅有一个实例二.如果你了解log4J的话,可以使用该类似方法,把log4j封装一下,实现静态方便的调用.
      

  2.   

    package com.vqq.comm;import org.apache.log4j.BasicConfigurator;
    import org.apache.log4j.Logger;
    import org.apache.log4j.ConsoleAppender;public class Tax{
    Logger logger = Logger.getLogger(Tax.class);
    private final double basePayTax = 1600;
    Tax(){
    BasicConfigurator.configure(); }

    public double getTax(double pay){
    logger.debug("This is debug."); 

    logger.info("This is an info."); 
    logger.warn("This is a warning."); 
    logger.error("This is an error."); 
    logger.fatal("This is a fatal error."); 
    Integer i=new Integer(1);
    i = new Integer(i.intValue()+1);

    return (pay-basePayTax)*0.05;
    }

    public static void main(String[] args){
    new Tax().getTax(33.33);
    }
    }
      

  3.   

    欢迎参加JAVA2005群:6276733
    要求二年工作经验!
      

  4.   

    能不能说说为什么会有这两行警告:
    log4j:WARN No appenders could be found for logger (DialogPanel).
    log4j:WARN Please initialize the log4j system properly.
    如何解决.
      

  5.   

    你要有一个Log4j的属性文件设定 log的往什么地方,以什么格式输出
    默认好像是 Log4j.properties
    如果实在应用程序中使用 只要 该文件只要放在 项目根目录就可以了
    属性文件具体的内容 上网搜索一下
      

  6.   

    log4j对程序速度的影响太大了,而且要写代码,我一直使用JDBMonitor来做数据库日志,根本不用写代码,爽呆了。宣称10秒为任意数据库增加执行日志功能
    我在这下载的:http://www.cownew.com/JDBMonitor/