0
在以下代碼中,類MyCustomeFormatter
擴展爲Formatter
。它也覆蓋了format
消息。這種方法何時被調用?例如:何時調用格式化方法?
logger.log(Level.INFO,"This is an info message")
語句將消息記錄到指定的處理程序。但是什麼時候調用overriden
方法?
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
public class MyCustomFormatter extends Formatter {
public MyCustomFormatter() {
super();
}
public String format(LogRecord record) {
// Create a StringBuffer to contain the formatted record
// start with the date.
StringBuffer sb = new StringBuffer();
// Get the date from the LogRecord and add it to the buffer
Date date = new Date(record.getMillis());
sb.append(date.toString());
sb.append(" ");
// Get the level name and add it to the buffer
sb.append(record.getLevel().getName());
sb.append(" ");
// Get the formatted message (includes localization
// and substitution of paramters) and add it to the buffer
sb.append(formatMessage(record));
sb.append("\n");
return sb.toString();
}
}