API di registrazione Java che genera file di registro vuoti

Ho provato a seguire il tutorial sull’API di registrazione Java:

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

Ma i file generati sono vuoti (testati in Netbeans, Eclipse e nell’esecuzione del jar da cmd). I messaggi di log sono visualizzati solo nella console.

I seguenti sono i file usati nel progetto. Quale potrebbe essere la ragione di tale comportamento?

Progetto: 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(""); buf.append(""); if (rec.getLevel().intValue() >= Level.WARNING.intValue()) { buf.append(""); buf.append(rec.getLevel()); buf.append(""); } else { buf.append(rec.getLevel()); } buf.append(""); buf.append(""); buf.append(calcDate(rec.getMillis())); buf.append(' '); buf.append(formatMessage(rec)); buf.append('\n'); buf.append(""); buf.append("\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 "\n\n" + (new Date()) + "\n\n\n
\n" + "\n " + "" + "" + "" + "\n"; } // This method is called just after the handler using this // formatter is closed public String getTail(Handler h) { return "
LevelTimeLog Message
\n
\n\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(); } } 

Cambia la seguente riga (nel metodo de.vogella.logger.MyLogger.setup() ):

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

con:

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

Vedi altro:

  • Registrazione Java
  • Registrazione Java: configurazione
  • Framework di registrazione Java