本文整理汇总了Java中java.util.logging.XMLFormatter.format方法的典型用法代码示例。如果您正苦于以下问题:Java XMLFormatter.format方法的具体用法?Java XMLFormatter.format怎么用?Java XMLFormatter.format使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.util.logging.XMLFormatter
的用法示例。
在下文中一共展示了XMLFormatter.format方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testXMLFormatterDifference
import java.util.logging.XMLFormatter; //导入方法依赖的package包/类
/**
* test whether the result of LogFormatter is the same as XMLFormatter
* if there is no nested exception
*/
public void testXMLFormatterDifference(){
LogRecord rec = new LogRecord(Level.SEVERE, "PROBLEM");
LogFormatter logFormatter = new LogFormatter();
XMLFormatter xmlFormatter = new XMLFormatter();
String logResult = logFormatter.format(rec).replace("1000", "SEVERE");
String xmlResult = xmlFormatter.format(rec);
assertEquals("WITHOUT THROWABLE", xmlResult, logResult);
rec.setThrown(new NullPointerException("TESTING EXCEPTION"));
rec.setResourceBundleName("MUJ BUNDLE");
logResult = logFormatter.format(rec);
//remove file names
logResult = logResult.replaceAll(" <file>.*</file>\n", "").replace("1000", "SEVERE");
xmlResult = xmlFormatter.format(rec);
assertEquals("WITH THROWABLE", xmlResult, logResult);
}
示例2: main
import java.util.logging.XMLFormatter; //导入方法依赖的package包/类
/**
* Before the fix, JDK8 prints: {@code
* <record>
* <date>3913-11-18T17:35:40</date>
* <millis>1384792540403</millis>
* <sequence>0</sequence>
* <level>INFO</level>
* <thread>1</thread>
* <message>test</message>
* </record>
* }
* After the fix, it should print: {@code
* <record>
* <date>2013-11-18T17:35:40</date>
* <millis>1384792696519</millis>
* <sequence>0</sequence>
* <level>INFO</level>
* <thread>1</thread>
* <message>test</message>
* </record>
* }
* @param args the command line arguments
*/
public static void main(String[] args) {
Locale locale = Locale.getDefault();
try {
Locale.setDefault(Locale.ENGLISH);
final GregorianCalendar cal1 = new GregorianCalendar();
final int year1 = cal1.get(Calendar.YEAR);
LogRecord record = new LogRecord(Level.INFO, "test");
XMLFormatter formatter = new XMLFormatter();
final String formatted = formatter.format(record);
System.out.println(formatted);
final GregorianCalendar cal2 = new GregorianCalendar();
final int year2 = cal2.get(Calendar.YEAR);
if (year2 < 1900) {
throw new Error("Invalid system year: " + year2);
}
StringBuilder buf2 = new StringBuilder()
.append("<date>").append(year2).append("-");
if (!formatted.contains(buf2.toString())) {
StringBuilder buf1 = new StringBuilder()
.append("<date>").append(year1).append("-");
if (formatted.contains(buf1)
&& year2 == year1 + 1
&& cal2.get(Calendar.MONTH) == Calendar.JANUARY
&& cal2.get(Calendar.DAY_OF_MONTH) == 1) {
// Oh! The year just switched in the midst of the test...
System.out.println("Happy new year!");
} else {
throw new Error("Expected year " + year2
+ " not found in log:\n" + formatted);
}
}
} finally {
Locale.setDefault(locale);
}
}
示例3: test
import java.util.logging.XMLFormatter; //导入方法依赖的package包/类
static void test(Supplier<TimeStamp> timeStampSupplier) {
TimeStamp t1 = timeStampSupplier.get();
int year1 = t1.getYear();
LogRecord record = new LogRecord(Level.INFO, "test");
XMLFormatter formatter = new XMLFormatter();
final String formatted = formatter.format(record);
System.out.println(formatted);
final TimeStamp t2 = timeStampSupplier.get();
final int year2 = t2.getYear();
if (year2 < 1900) {
throw new Error("Invalid system year: " + year2);
}
final StringBuilder buf2 = new StringBuilder()
.append("<date>").append(year2).append("-");
if (!formatted.contains(buf2.toString())) {
StringBuilder buf1 = new StringBuilder()
.append("<date>").append(year1).append("-");
if (formatted.contains(buf1) && year2 == year1 + 1
&& t2.isJanuaryFirst()) {
// Oh! The year just switched in the midst of the test...
System.out.println("Happy new year!");
} else {
throw new Error("Expected year " + year2
+ " not found in log:\n" + formatted);
}
}
}
示例4: testMatching
import java.util.logging.XMLFormatter; //导入方法依赖的package包/类
private static void testMatching(XMLFormatter formatter,
LogRecord record, Instant instant, long expectedNanos,
boolean useInstant) {
ZonedDateTime zdt = ZonedDateTime.ofInstant(instant, ZoneId.systemDefault());
int zdtNanos = zdt.getNano();
assertEquals(expectedNanos, zdtNanos, "ZonedDateTime.getNano()");
String str = formatter.format(record);
String match = "."+expectedNanos;
if (str.contains(match) != useInstant) {
throw new RuntimeException(formatter.getClass().getSimpleName()
+ ".format()"
+ " string does not contain expected nanos: "
+ "\n\texpected match for: '" + match + "'"
+ "\n\tin: \n" + str);
}
System.out.println("Found expected match for '"+match+"' in \n"+str);
match = "<millis>"+instant.toEpochMilli()+"</millis>";
if (!str.contains(match)) {
throw new RuntimeException(formatter.getClass().getSimpleName()
+ ".format()"
+ " string does not contain expected millis: "
+ "\n\texpected match for: '" + match + "'"
+ "\n\tin: \n" + str);
}
System.out.println("Found expected match for '"+match+"' in \n"+str);
match = "<nanos>";
if (str.contains(match) != useInstant) {
throw new RuntimeException(formatter.getClass().getSimpleName()
+ ".format()"
+ " string "
+ (useInstant
? "does not contain expected nanos: "
: "contains unexpected nanos: ")
+ "\n\t" + (useInstant ? "expected" : "unexpected")
+ " match for: '" + match + "'"
+ "\n\tin: \n" + str);
}
match = "<nanos>"+getNanoAdjustment(record)+"</nanos>";
if (str.contains(match) != useInstant) {
throw new RuntimeException(formatter.getClass().getSimpleName()
+ ".format()"
+ " string "
+ (useInstant
? "does not contain expected nanos: "
: "contains unexpected nanos: ")
+ "\n\t" + (useInstant ? "expected" : "unexpected")
+ " match for: '" + match + "'"
+ "\n\tin: \n" + str);
}
if (useInstant) {
System.out.println("Found expected match for '"+match+"' in \n"+str);
} else {
System.out.println("As expected '"+match+"' is not present in \n"+str);
}
match = useInstant ? DateTimeFormatter.ISO_INSTANT.format(instant)
: zdt.truncatedTo(ChronoUnit.SECONDS)
.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME);
match = "<date>"+match+"</date>";
if (!str.contains(match)) {
throw new RuntimeException(formatter.getClass().getSimpleName()
+ ".format()"
+ " string does not contain expected date: "
+ "\n\texpected match for: '" + match + "'"
+ "\n\tin: \n" + str);
}
System.out.println("Found expected match for '"+match+"' in \n"+str);
}