Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Logging API generating empty log files

I tried to go through the tutorial on the Java logging API:

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

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.

The following are the files used in the project. What might be the reason for such behavior?

Project: 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);
  }
} 

UseLogger.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();
  }
} 
like image 331
asliwinski Avatar asked May 20 '13 02:05

asliwinski


1 Answers

Change the following line (in the method de.vogella.logger.MyLogger.setup()):

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

with:

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

See more:

  • Java Logging
  • Java Logging: Configuration
  • Java Logging Framework
like image 182
Paul Vargas Avatar answered Nov 14 '22 22:11

Paul Vargas