title: Java Logging API generating empty log files
body: <p>I tried to go through the tutorial on the Java logging API:</p>
  
  <p>www.vogella.com/articles/Logging/article.html</p>
  
  <p>But the generated files are empty (tested in Netbeans, Eclipse as well as running the jar from cmd). The log messages are displayed in the console only.</p>
  
  <p>The following are the files used in the project. What might be the reason for such behavior?</p>
  
  <p>Project: de.vogella.logger</p>
  
  <p>MyHtmlFormatter.java</p>
  
  <pre><code>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 &gt;= WARNING
      buf.append("&lt;tr&gt;");
      buf.append("&lt;td&gt;");
  
      if (rec.getLevel().intValue() &gt;= Level.WARNING.intValue()) {
        buf.append("&lt;b&gt;");
        buf.append(rec.getLevel());
        buf.append("&lt;/b&gt;");
      } else {
        buf.append(rec.getLevel());
      }
      buf.append("&lt;/td&gt;");
      buf.append("&lt;td&gt;");
      buf.append(calcDate(rec.getMillis()));
      buf.append(' ');
      buf.append(formatMessage(rec));
      buf.append('\n');
      buf.append("&lt;td&gt;");
      buf.append("&lt;/tr&gt;\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 "&lt;HTML&gt;\n&lt;HEAD&gt;\n" + (new Date()) 
          + "\n&lt;/HEAD&gt;\n&lt;BODY&gt;\n&lt;PRE&gt;\n"
          + "&lt;table width=\"100%\" border&gt;\n  "
          + "&lt;tr&gt;&lt;th&gt;Level&lt;/th&gt;" +
          "&lt;th&gt;Time&lt;/th&gt;" +
          "&lt;th&gt;Log Message&lt;/th&gt;" +
          "&lt;/tr&gt;\n";
    }
  
    // This method is called just after the handler using this
    // formatter is closed
    public String getTail(Handler h) {
      return "&lt;/table&gt;\n  &lt;/PRE&gt;&lt;/BODY&gt;\n&lt;/HTML&gt;\n";
    }
  }
  </code></pre>
  
  <p>MyLogger.java</p>
  
  <pre><code>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);
    }
  } 
  </code></pre>
  
  <p>UseLogger.java</p>
  
  <pre><code>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();
    }
  } 
  </code></pre>