下面列出了java.util.logging.LogRecord#setThreadID ( ) 实例代码,或者点击链接到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));
}
@SuppressWarnings("deprecation") // setMillis is deprecated in JDK 9
protected Object writeReplace() {
LogRecord serialized = new LogRecord(getLevel(), getMessage());
serialized.setLoggerName(getLoggerName());
serialized.setResourceBundle(getResourceBundle());
serialized.setResourceBundleName(getResourceBundleName());
serialized.setSourceClassName(getSourceClassName());
serialized.setSourceMethodName(getSourceMethodName());
serialized.setSequenceNumber(getSequenceNumber());
serialized.setParameters(getParameters());
serialized.setThreadID(getThreadID());
serialized.setMillis(getMillis());
serialized.setThrown(getThrown());
return serialized;
}
@SuppressWarnings("deprecation") // setMillis is deprecated in JDK 9
protected Object writeReplace() {
LogRecord serialized = new LogRecord(getLevel(), getMessage());
serialized.setLoggerName(getLoggerName());
serialized.setResourceBundle(getResourceBundle());
serialized.setResourceBundleName(getResourceBundleName());
serialized.setSourceClassName(getSourceClassName());
serialized.setSourceMethodName(getSourceMethodName());
serialized.setSequenceNumber(getSequenceNumber());
serialized.setParameters(getParameters());
serialized.setThreadID(getThreadID());
serialized.setMillis(getMillis());
serialized.setThrown(getThrown());
return serialized;
}
@SuppressWarnings("deprecation") // setMillis is deprecated in JDK 9
protected Object writeReplace() {
LogRecord serialized = new LogRecord(getLevel(), getMessage());
serialized.setLoggerName(getLoggerName());
serialized.setResourceBundle(getResourceBundle());
serialized.setResourceBundleName(getResourceBundleName());
serialized.setSourceClassName(getSourceClassName());
serialized.setSourceMethodName(getSourceMethodName());
serialized.setSequenceNumber(getSequenceNumber());
serialized.setParameters(getParameters());
serialized.setThreadID(getThreadID());
serialized.setMillis(getMillis());
serialized.setThrown(getThrown());
return serialized;
}
/**
* Creates and returns a LogRecord with a given message and throwable.
*
* @param message The message to place in the {@link LogRecord}
* @param throwable The throwable to place in the {@link LogRecord}
* @param params A list of parameters to place in the {@link LogRecord}
* @return A {@link LogRecord} with the given message and throwable.
*/
private LogRecord createLogRecord(String message, Throwable throwable, Object... params) {
LogRecord logRecord = new LogRecord(Level.INFO, message);
logRecord.setLoggerName("LoggerName");
logRecord.setMillis(1L);
logRecord.setThreadID(2);
logRecord.setThrown(throwable);
logRecord.setParameters(params);
return logRecord;
}
private static LogRecord createLogRecord(
Level level, ZonedDateTime dateTime, RuntimeException thrown) {
LogRecord record = new LogRecord(level, "some message");
record.setMillis(dateTime.toInstant().toEpochMilli());
record.setSourceClassName("SomeSourceClass");
record.setSourceMethodName("aSourceMethod");
record.setThreadID(543);
if (thrown != null) {
record.setThrown(thrown);
}
return record;
}
/**
* {@inheritDoc}
* <p>
* Overridden to create a {@link LogRecord} based on the log information.
* </p>
*
* @see Log#log(Level, Throwable, String, Object[])
*/
@Override
protected final void doLog(final Level level, final Throwable thrown,
final String template, final Object... args) {
if (myDelegate.isLoggable(level)) {
final Thread currentThread = Thread.currentThread();
final LogRecord record = new LogRecord(level,
format(template, args));
record.setLoggerName(myDelegate.getName());
record.setThrown(thrown);
record.setThreadID((int) Thread.currentThread().getId());
// Note the name of the class is the AbstractLog which is where all
// of the public log methods are implemented.
boolean lookingForThisClass = true;
for (final StackTraceElement element : currentThread
.getStackTrace()) {
final String className = element.getClassName();
// Find this method (and maybe others from this class).
if (lookingForThisClass) {
lookingForThisClass = !CLASS_NAME.equals(className);
}
else {
// And back until we are past this class.
if (!CLASS_NAME.equals(className)) {
record.setSourceClassName(className);
record.setSourceMethodName(element.getMethodName());
break;
}
}
}
// Finally - log it.
myDelegate.log(record);
}
}
private static LogRecord logRecord(
Level level, String message, String loggerName, int tid, long millis) {
LogRecord result = new LogRecord(level, message);
result.setLoggerName(loggerName);
result.setMillis(millis);
result.setThreadID(tid);
return result;
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (current != null) {
String v = chars.toString();
values.put(current, v);
if (current == Elem.PARAM) {
if (params == null) {
params = new ArrayList<String>();
}
params.add(v);
if (params.size() > 1500) {
LOG.severe("Too long params when reading a record. Deleting few. Msg: " + Elem.MESSAGE.parse(values)); // NOI18N
for (String p : params) {
LOG.fine(p);
}
params.clear();
}
}
}
current = null;
chars = new StringBuilder();
if (currentEx != null && currentEx.values != null) {
if ("frame".equals(qName)) { // NOI18N
String line = Elem.LINE.parse(values);
StackTraceElement elem = new StackTraceElement(
Elem.CLASS.parse(values),
Elem.METHOD.parse(values),
Elem.FILE.parse(values),
line == null ? -1 : Integer.parseInt(line)
);
currentEx.trace.add(elem);
values.remove(Elem.CLASS);
values.remove(Elem.METHOD);
values.remove(Elem.LINE);
}
if ("exception".equals(qName)) {
currentEx.message = values.get(Elem.MESSAGE);
String more = values.get(Elem.MORE);
if (more != null) currentEx.more = Integer.parseInt(more);
if (exceptions == null){
exceptions = new ArrayDeque<FakeException>();
}
exceptions.add(currentEx);
values = currentEx.values;
currentEx = null;
}
return;
}
if ("record".equals(qName)) { // NOI18N
String millis = Elem.MILLIS.parse(values);
String seq = Elem.SEQUENCE.parse(values);
String lev = Elem.LEVEL.parse(values);
String thread = Elem.THREAD.parse(values);
String msg = Elem.MESSAGE.parse(values);
String key = Elem.KEY.parse(values);
String catalog = Elem.CATALOG.parse(values);
if (lev != null) {
LogRecord r = new LogRecord(parseLevel(lev), key != null && catalog != null ? key : msg);
try {
r.setThreadID(parseInt(thread));
} catch (NumberFormatException ex) {
LOG.log(Level.WARNING, ex.getMessage(), ex);
}
r.setSequenceNumber(parseLong(seq));
r.setMillis(parseLong(millis));
r.setResourceBundleName(key);
if (catalog != null && key != null) {
r.setResourceBundleName(catalog);
if (!"<null>".equals(catalog)) { // NOI18N
try {
ResourceBundle b = NbBundle.getBundle(catalog);
b.getObject(key);
// ok, the key is there
r.setResourceBundle(b);
} catch (MissingResourceException e) {
LOG.log(Level.CONFIG, "Cannot find resource bundle {0} for key {1}", new Object[] { catalog, key });
r.setResourceBundle(new FakeBundle(key, msg));
}
} else {
LOG.log(Level.CONFIG, "Cannot find resource bundle <null> for key {1}", key);
}
}
if (params != null) {
r.setParameters(params.toArray());
}
if (exceptions != null) {
r.setThrown(createThrown(null));
// exceptions = null; should be empty after poll
}
callback.publish(r);
}
currentEx = null;
params = null;
values.clear();
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (current != null) {
String v = chars.toString();
values.put(current, v);
if (current == Elem.PARAM) {
if (params == null) {
params = new ArrayList<String>();
}
params.add(v);
if (params.size() > 1500) {
LOG.severe("Too long params when reading a record. Deleting few. Msg: " + Elem.MESSAGE.parse(values)); // NOI18N
for (String p : params) {
LOG.fine(p);
}
params.clear();
}
}
}
current = null;
chars = new StringBuilder();
if (currentEx != null && currentEx.values != null) {
if ("frame".equals(qName)) { // NOI18N
String line = Elem.LINE.parse(values);
StackTraceElement elem = new StackTraceElement(
Elem.CLASS.parse(values),
Elem.METHOD.parse(values),
Elem.FILE.parse(values),
line == null ? -1 : Integer.parseInt(line)
);
currentEx.trace.add(elem);
values.remove(Elem.CLASS);
values.remove(Elem.METHOD);
values.remove(Elem.LINE);
}
if ("exception".equals(qName)) {
currentEx.message = values.get(Elem.MESSAGE);
String more = values.get(Elem.MORE);
if (more != null) currentEx.more = Integer.parseInt(more);
if (exceptions == null){
exceptions = new ArrayDeque<FakeException>();
}
exceptions.add(currentEx);
values = currentEx.values;
currentEx = null;
}
return;
}
if ("record".equals(qName)) { // NOI18N
String millis = Elem.MILLIS.parse(values);
String seq = Elem.SEQUENCE.parse(values);
String lev = Elem.LEVEL.parse(values);
String thread = Elem.THREAD.parse(values);
String msg = Elem.MESSAGE.parse(values);
String key = Elem.KEY.parse(values);
String catalog = Elem.CATALOG.parse(values);
if (lev != null) {
LogRecord r = new LogRecord(parseLevel(lev), key != null && catalog != null ? key : msg);
try {
r.setThreadID(parseInt(thread));
} catch (NumberFormatException ex) {
LOG.log(Level.WARNING, ex.getMessage(), ex);
}
r.setSequenceNumber(parseLong(seq));
r.setMillis(parseLong(millis));
r.setResourceBundleName(key);
if (catalog != null && key != null) {
r.setResourceBundleName(catalog);
if (!"<null>".equals(catalog)) { // NOI18N
try {
ResourceBundle b = NbBundle.getBundle(catalog);
b.getObject(key);
// ok, the key is there
r.setResourceBundle(b);
} catch (MissingResourceException e) {
LOG.log(Level.CONFIG, "Cannot find resource bundle {0} for key {1}", new Object[] { catalog, key });
r.setResourceBundle(new FakeBundle(key, msg));
}
} else {
LOG.log(Level.CONFIG, "Cannot find resource bundle <null> for key {1}", key);
}
}
if (params != null) {
r.setParameters(params.toArray());
}
if (exceptions != null) {
r.setThrown(createThrown(null));
// exceptions = null; should be empty after poll
}
callback.publish(r);
}
currentEx = null;
params = null;
values.clear();
}
}
@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"));
}
private static LogRecord newLogRecordWithThreadId(Level level, String contents, int threadId) {
LogRecord result = new LogRecord(level, contents);
result.setThreadID(threadId);
return result;
}