7

我尝试阅读有关 Java 日志记录 API 的教程:

www.vogella.com/articles/Logging/article.html

但是生成的文件是空的(在 Netbeans、Eclipse 以及从 cmd 运行 jar 测试)。日志消息仅显示在控制台中。

以下是项目中使用的文件。这种行为的原因可能是什么?

项目:de.vogella.logger

MyHtmlFormatter.java

package de.vogella.logger;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;

//This custom formatter formats parts of a log record to a single line
class MyHtmlFormatter extends Formatter {
  // This method is called for every log records
  public String format(LogRecord rec) {
    StringBuffer buf = new StringBuffer(1000);
    // Bold any levels >= WARNING
    buf.append("<tr>");
    buf.append("<td>");

    if (rec.getLevel().intValue() >= Level.WARNING.intValue()) {
      buf.append("<b>");
      buf.append(rec.getLevel());
      buf.append("</b>");
    } else {
      buf.append(rec.getLevel());
    }
    buf.append("</td>");
    buf.append("<td>");
    buf.append(calcDate(rec.getMillis()));
    buf.append(' ');
    buf.append(formatMessage(rec));
    buf.append('\n');
    buf.append("<td>");
    buf.append("</tr>\n");
    return buf.toString();
  }

  private String calcDate(long millisecs) {
    SimpleDateFormat date_format = new SimpleDateFormat("MMM dd,yyyy HH:mm");
    Date resultdate = new Date(millisecs);
    return date_format.format(resultdate);
  }

  // This method is called just after the handler using this
  // formatter is created
  public String getHead(Handler h) {
    return "<HTML>\n<HEAD>\n" + (new Date()) 
        + "\n</HEAD>\n<BODY>\n<PRE>\n"
        + "<table width=\"100%\" border>\n  "
        + "<tr><th>Level</th>" +
        "<th>Time</th>" +
        "<th>Log Message</th>" +
        "</tr>\n";
  }

  // This method is called just after the handler using this
  // formatter is closed
  public String getTail(Handler h) {
    return "</table>\n  </PRE></BODY>\n</HTML>\n";
  }
}

MyLogger.java

package de.vogella.logger;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class MyLogger {
  static private FileHandler fileTxt;
  static private SimpleFormatter formatterTxt;

  static private FileHandler fileHTML;
  static private Formatter formatterHTML;

  static public void setup() throws IOException {

    // Get the global logger to configure it
    Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

    logger.setLevel(Level.INFO);
    fileTxt = new FileHandler("Logging.txt");
    fileHTML = new FileHandler("Logging.html");

    // Create txt Formatter
    formatterTxt = new SimpleFormatter();
    fileTxt.setFormatter(formatterTxt);
    logger.addHandler(fileTxt);

    // Create HTML Formatter
    formatterHTML = new MyHtmlFormatter();
    fileHTML.setFormatter(formatterHTML);
    logger.addHandler(fileHTML);
  }
} 

使用Logger.java

package de.vogella.logger.test;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import de.vogella.logger.MyLogger;

public class UseLogger {
  // Always use the classname, this way you can refactor
  private final static Logger LOGGER = Logger.getLogger(UseLogger.class
      .getName());

  public void doSomeThingAndLog() {
    // Image here some real work

    // Now we demo the logging

    // Set the LogLevel to Severe, only severe Messages will be written
    LOGGER.setLevel(Level.SEVERE);
    LOGGER.severe("Info Log");
    LOGGER.warning("Info Log");
    LOGGER.info("Info Log");
    LOGGER.finest("Really not important");

    // Set the LogLevel to Info, severe, warning and info will be written
    // Finest is still not written
    LOGGER.setLevel(Level.INFO);
    LOGGER.severe("Info Log");
    LOGGER.warning("Info Log");
    LOGGER.info("Info Log");
    LOGGER.finest("Really not important");
  }

  public static void main(String[] args) {
    UseLogger tester = new UseLogger();
    try {
      MyLogger.setup();
    } catch (IOException e) {
      e.printStackTrace();
      throw new RuntimeException("Problems with creating the log files");
    }
    tester.doSomeThingAndLog();
  }
} 
4

1 回答 1

8

更改以下行(在方法中de.vogella.logger.MyLogger.setup()):

// Get the global logger to configure it
Logger logger = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME);

和:

// Get the global logger to configure it
Logger logger = Logger.getLogger("");

看更多:

于 2013-05-20T06:37:46.920 回答