本文整理匯總了Java中java.util.logging.SimpleFormatter.format方法的典型用法代碼示例。如果您正苦於以下問題:Java SimpleFormatter.format方法的具體用法?Java SimpleFormatter.format怎麽用?Java SimpleFormatter.format使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.logging.SimpleFormatter
的用法示例。
在下文中一共展示了SimpleFormatter.format方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: dotest
import java.util.logging.SimpleFormatter; //導入方法依賴的package包/類
/**
* Deserializes the Base64 encoded string returned by {@link
* #getBase64()}, format the obtained LogRecord using a
* SimpleFormatter, and checks that the string representation obtained
* matches the original string representation returned by {@link
* #getString()}.
*/
protected void dotest() {
try {
final String base64 = getBase64();
final ByteArrayInputStream bais =
new ByteArrayInputStream(Base64.getDecoder().decode(base64));
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record = (LogRecord)ois.readObject();
final SimpleFormatter formatter = new SimpleFormatter();
String expected = getString();
String str2 = formatter.format(record);
check(expected, str2);
System.out.println(str2);
System.out.println("PASSED: "+this.getClass().getName()+"\n");
} catch (IOException | ClassNotFoundException x) {
throw new RuntimeException(x);
}
}
示例2: test
import java.util.logging.SimpleFormatter; //導入方法依賴的package包/類
/**
* Serializes a log record, then deserializes it and check that both
* records match.
* @param record the log record to serialize & deserialize.
* @param hasExceedingNanos whether the record has a nano adjustment whose
* value exceeds 1ms.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static void test(LogRecord record, boolean hasExceedingNanos)
throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// First checks that the log record can be deserialized
final ByteArrayInputStream bais =
new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
assertEquals(record.getMillis(), record2.getMillis(), "getMillis()");
assertEquals(record.getInstant().getEpochSecond(),
record2.getInstant().getEpochSecond(),
"getInstant().getEpochSecond()");
assertEquals(record.getInstant().getNano(),
record2.getInstant().getNano(),
"getInstant().getNano()");
assertEquals(record.getInstant().toEpochMilli(),
record2.getInstant().toEpochMilli(),
"getInstant().toEpochMilli()");
long millis = record.getMillis();
millis = hasExceedingNanos
? Instant.ofEpochSecond(millis/MILLIS_IN_SECOND,
(millis%MILLIS_IN_SECOND)*NANOS_IN_MILLI
+ record.getInstant().getNano() % NANOS_IN_MILLI).toEpochMilli()
: millis;
assertEquals(millis, record.getInstant().toEpochMilli(),
"getMillis()/getInstant().toEpochMilli()");
millis = record2.getMillis();
millis = hasExceedingNanos
? Instant.ofEpochSecond(millis/MILLIS_IN_SECOND,
(millis%MILLIS_IN_SECOND)*NANOS_IN_MILLI
+ record2.getInstant().getNano() % NANOS_IN_MILLI).toEpochMilli()
: millis;
assertEquals(millis, record2.getInstant().toEpochMilli(),
"getMillis()/getInstant().toEpochMilli()");
long nanos = nanoInSecondFromEpochMilli(record.getMillis())
+ record.getInstant().getNano() % NANOS_IN_MILLI;
assertEquals(nanos, record.getInstant().getNano(),
"nanoInSecondFromEpochMilli(record.getMillis())"
+ " + record.getInstant().getNano() % NANOS_IN_MILLI /getInstant().getNano()");
nanos = nanoInSecondFromEpochMilli(record2.getMillis())
+ record2.getInstant().getNano() % NANOS_IN_MILLI;
assertEquals(nanos, record2.getInstant().getNano(),
"nanoInSecondFromEpochMilli(record2.getMillis())"
+ " + record2.getInstant().getNano() % NANOS_IN_MILLI /getInstant().getNano()");
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2))
throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
}
示例3: test
import java.util.logging.SimpleFormatter; //導入方法依賴的package包/類
/**
* Serializes a log record, then deserializes it and check that both
* records match.
* @param record the log record to serialize & deserialize.
* @throws IOException Unexpected.
* @throws ClassNotFoundException Unexpected.
*/
public static void test(LogRecord record)
throws IOException, ClassNotFoundException {
// Format the given logRecord using the SimpleFormatter
SimpleFormatter formatter = new SimpleFormatter();
String str = formatter.format(record);
// Serialize the given LogRecord
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(record);
oos.flush();
oos.close();
// First checks that the log record can be deserialized
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
final ObjectInputStream ois = new ObjectInputStream(bais);
final LogRecord record2 = (LogRecord)ois.readObject();
assertEquals(record.getMillis(), record2.getMillis(), "getMillis()");
assertEquals(record.getInstant().getEpochSecond(),
record2.getInstant().getEpochSecond(),
"getInstant().getEpochSecond()");
assertEquals(record.getInstant().getNano(),
record2.getInstant().getNano(),
"getInstant().getNano()");
assertEquals(record.getInstant().toEpochMilli(),
record2.getInstant().toEpochMilli(),
"getInstant().toEpochMilli()");
assertEquals(record.getMillis(),
record.getInstant().toEpochMilli(),
"getMillis()/getInstant().toEpochMilli()");
assertEquals(record2.getMillis(),
record2.getInstant().toEpochMilli(),
"getMillis()/getInstant().toEpochMilli()");
assertEquals((record.getMillis()%MILLIS_IN_SECOND)*NANOS_IN_MILLI
+ (record.getInstant().getNano() % NANOS_IN_MILLI),
record.getInstant().getNano(),
"record.getMillis()%1000)*1000_000"
+ " + record.getInstant().getNano()%1000_000 / getInstant().getNano()");
assertEquals((record2.getMillis()%MILLIS_IN_SECOND)*NANOS_IN_MILLI
+ (record2.getInstant().getNano() % NANOS_IN_MILLI),
record2.getInstant().getNano(),
"record2.getMillis()%1000)*1000_000"
+ " + record2.getInstant().getNano()%1000_000 / getInstant().getNano()");
// Format the deserialized LogRecord using the SimpleFormatter, and
// check that the string representation obtained matches the string
// representation of the original LogRecord
String str2 = formatter.format(record2);
if (!str.equals(str2))
throw new RuntimeException("Unexpected values in deserialized object:"
+ "\n\tExpected: " + str
+ "\n\tRetrieved: "+str);
}