下面列出了java.util.logging.LogRecord#setInstant ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* test that {0} etc is replaced properly
*/
@Test
public void testTextFormatting () {
VespaFormatter formatter = new VespaFormatter(serviceName, app);
LogRecord testRecord = new LogRecord(Level.INFO, "this {1} is {0} test");
testRecord.setInstant(Instant.ofEpochMilli(1098709021843L));
testRecord.setThreadID(123);
testRecord.setLoggerName("org.foo");
Object[] params = { "a small", "message" };
testRecord.setParameters(params);
String expected = "1098709021.843000\t"
+ hostname + "\t"
+ pid
+ "/123" + "\t"
+ "serviceName\t"
+ app
+ ".org.foo\t"
+ "info\t"
+ "this message is a small test\n";
assertEquals(expected, formatter.format(testRecord));
}
@Before
public void setUp() {
System.setProperty("config.id", "my-test-config-id");
System.setProperty("vespa.log.target", "file:test3");
zookeeperLogRecord = new LogRecord(Level.WARNING, "zookeeper log record");
zookeeperLogRecord.setLoggerName("org.apache.zookeeper.server.NIOServerCnxn");
zookeeperLogRecord.setInstant(Instant.ofEpochMilli(1107011348029L));
curatorLogRecord = new LogRecord(Level.WARNING, "curator log record");
curatorLogRecord.setLoggerName("org.apache.curator.utils.DefaultTracerDriver");
curatorLogRecord.setInstant(Instant.ofEpochMilli(1107011348029L));
hostname = Util.getHostName();
pid = Util.getPID();
zookeeperLogRecordString = "1107011348.029000\t"
+ hostname
+ "\t"
+ pid
+ "/" + zookeeperLogRecord.getThreadID() + "\t-\t.org.apache.zookeeper.server.NIOServerCnxn"
+ "\twarning\tzookeeper log record";
zookeeperLogRecordError = new LogRecord(Level.SEVERE, "zookeeper error");
zookeeperLogRecordError.setLoggerName("org.apache.zookeeper.server.NIOServerCnxn");
zookeeperLogRecordError.setInstant(Instant.ofEpochMilli(1107011348029L));
curatorLogRecordError = new LogRecord(Level.SEVERE, "curator log record");
curatorLogRecordError.setLoggerName("org.apache.curator.utils.DefaultTracerDriver");
curatorLogRecordError.setInstant(Instant.ofEpochMilli(1107011348029L));
notzookeeperLogRecord = new LogRecord(Level.WARNING, "not zookeeper log record");
notzookeeperLogRecord.setLoggerName("org.apache.foo.Bar");
notzookeeperLogRecord.setInstant(Instant.ofEpochMilli(1107011348029L));
}
private static LogRecord createRecord(Exception exception) {
LogRecord record = new LogRecord(Level.INFO, "Hello.");
record.setSequenceNumber(1);
record.setInstant(Instant.ofEpochMilli(2));
record.setThrown(exception);
return record;
}
static void setNanoAdjustment(LogRecord record, int nanos) {
record.setInstant(Instant.ofEpochSecond(record.getInstant().getEpochSecond(),
(record.getInstant().getNano() / NANOS_IN_MILLI) * NANOS_IN_MILLI + nanos));
}
static void setNanoAdjustment(LogRecord record, int nanos) {
record.setInstant(Instant.ofEpochSecond(record.getInstant().getEpochSecond(),
(record.getInstant().getNano() / NANOS_IN_MILLI) * NANOS_IN_MILLI + nanos));
}
@Before
public void setUp () {
hostname = Util.getHostName();
pid = Util.getPID();
testRecord1 = new LogRecord(Level.INFO, "this is a test");
testRecord1.setInstant(Instant.ofEpochMilli(1098709021843L));
testRecord1.setThreadID(123);
expected1 = "1098709021.843000\t"
+ hostname + "\t"
+ pid
+ "/123" + "\t"
+ "serviceName\t"
+ "tst\t"
+ "info\t"
+ "this is a test\n";
expected2 = "1098709021.843000\t"
+ hostname + "\t"
+ pid
+ "/123" + "\t"
+ "serviceName\t"
+ "-\t"
+ "info\t"
+ "this is a test\n";
testRecord2 = new LogRecord(Level.INFO, "this is a test");
testRecord2.setInstant(Instant.ofEpochMilli(1098709021843L));
testRecord2.setThreadID(123);
testRecord2.setLoggerName("org.foo");
expected3 = "1098709021.843000\t"
+ hostname + "\t"
+ pid
+ "/123" + "\t"
+ "serviceName\t"
+ ".org.foo\t"
+ "info\t"
+ "this is a test\n";
expected4 = "1098709021.843000\t"
+ hostname + "\t"
+ pid
+ "/123" + "\t"
+ "serviceName\t"
+ "tst.org.foo\t"
+ "info\t"
+ "this is a test\n";
}
@Test
public void requireThatJdk14PropertiesAreAvailableThroughServiceReference() {
MyLogService logService = new MyLogService();
Logger log = newLogger(logService);
LogRecord record = new LogRecord(Level.INFO, "message");
record.setLoggerName("loggerName");
record.setInstant(Instant.ofEpochMilli(69));
Object[] parameters = new Object[0];
record.setParameters(parameters);
ResourceBundle resouceBundle = new MyResourceBundle();
record.setResourceBundle(resouceBundle);
record.setResourceBundleName("resourceBundleName");
record.setSequenceNumber(69);
record.setSourceClassName("sourceClassName");
record.setSourceMethodName("sourceMethodName");
record.setThreadID(69);
Throwable thrown = new Throwable();
record.setThrown(thrown);
log.log(record);
ServiceReference<?> ref = logService.lastServiceReference;
assertNotNull(ref);
assertTrue(Arrays.equals(new String[] { "LEVEL",
"LOGGER_NAME",
"MESSAGE",
"MILLIS",
"PARAMETERS",
"RESOURCE_BUNDLE",
"RESOURCE_BUNDLE_NAME",
"SEQUENCE_NUMBER",
"SOURCE_CLASS_NAME",
"SOURCE_METHOD_NAME",
"THREAD_ID",
"THROWN" },
ref.getPropertyKeys()));
assertEquals(Level.INFO, ref.getProperty("LEVEL"));
assertEquals("loggerName", ref.getProperty("LOGGER_NAME"));
assertEquals("message", ref.getProperty("MESSAGE"));
assertEquals(69L, ref.getProperty("MILLIS"));
assertSame(parameters, ref.getProperty("PARAMETERS"));
assertSame(resouceBundle, ref.getProperty("RESOURCE_BUNDLE"));
assertEquals("resourceBundleName", ref.getProperty("RESOURCE_BUNDLE_NAME"));
assertEquals(69L, ref.getProperty("SEQUENCE_NUMBER"));
assertEquals("sourceClassName", ref.getProperty("SOURCE_CLASS_NAME"));
assertEquals("sourceMethodName", ref.getProperty("SOURCE_METHOD_NAME"));
assertEquals(69, ref.getProperty("THREAD_ID"));
assertSame(thrown, ref.getProperty("THROWN"));
assertNull(ref.getProperty("unknown"));
}