I tried to go through the Java Logging API tutorial:
www.vogella.com/articles/Logging/article.html
But the generated files are empty (tested in Netbeans, Eclipse, as well as running jar from cmd). Log messages are displayed only in the console.
The following are the files used in the project. What could be causing this 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 {
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 {
source share