下面列出了java.util.logging.LogRecord#setLevel ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public boolean isLoggable(LogRecord record) {
// Only allow filtering messages of warning level and lower
if (record.getLevel().intValue() > Level.WARNING.intValue()) {
return true;
}
LogCleanupFilterElement filterElement = filterElements.get(record.getLoggerName());
if (filterElement != null) {
for (String messageStart : filterElement.getMessageStarts()) {
if (record.getMessage().startsWith(messageStart)) {
record.setLevel(org.jboss.logmanager.Level.DEBUG);
return Logger.getLogger(record.getLoggerName()).isDebugEnabled();
}
}
}
return true;
}
/**
* Invoked when the JVM is shutting down, or when the Servlet or OSGi bundle is uninstalled.
* This method shutdowns the Derby database.
*
* @throws ReflectiveOperationException if an error occurred while
* setting the shutdown property on the Derby data source.
*/
private static synchronized void shutdown() throws ReflectiveOperationException {
final DataSource ds = source;
if (ds != null) { // Should never be null, but let be safe.
source = null; // Clear now in case of failure in remaining code.
connected = false;
ds.getClass().getMethod("setShutdownDatabase", String.class).invoke(ds, "shutdown");
try {
ds.getConnection().close(); // Does the actual shutdown.
} catch (SQLException e) { // This is the expected exception.
final LogRecord record = new LogRecord(Level.FINE, e.getMessage());
if (!isSuccessfulShutdown(e)) {
record.setLevel(Level.WARNING);
record.setThrown(e);
}
record.setLoggerName(Loggers.SQL);
Logging.log(Initializer.class, "shutdown", record);
}
}
}
@Test
public void format() {
final LogRecord record = new LogRecord(Level.FINE, "test message");
record.setLoggerName("logger");
record.setLevel(Level.FINER);
record.setMillis(123456789);
record.setSourceClassName("my.class.Name");
record.setSourceMethodName("aMethod");
// default
assertEquals(
"Jan 02, 1970 my.class.Name aMethod\nFINER: test message\n",
new LocalFileHandler.PatternFormatter("%1$tb %1$td, %1$tY %1$tl:%1$tM:%1$tS %1$Tp %2$s%n%4$s: %5$s%6$s%n", Locale.ENGLISH)
.format(record).replace("\r", "").replaceFirst("1970.*my"/*skip time*/, "1970 my"));
// simple
assertEquals(
"test message\n",
new LocalFileHandler.PatternFormatter("%5$s%n", Locale.ENGLISH).format(record).replace("\r", ""));
final String custom = new LocalFileHandler.PatternFormatter("%1$tY-%1$tM-%1$td %1$tT [%4$5s][%7$s] %5$s%6$s%n", Locale.ENGLISH)
.format(record).replace("\r", "");
assertTrue(custom
.matches("1970\\-17\\-02 \\p{Digit}+\\:17\\:36 \\[FINER\\]\\[my\\.class\\.Name\\] test message\\\n"));
}
@Override
public void log(LogRecord record) {
record.setMessage(this.prefix + record.getMessage());
// Don't trust loggers to show anything below INFO.
// Check the level ourselves and then promote the record
// to make sure it gets through.
if (record.getLevel().intValue() < Level.INFO.intValue()
&& record.getLevel().intValue() >= this.getLevel().intValue()) {
record.setLevel(Level.INFO);
}
super.log(record);
}
@Override
public void log(LogRecord record) {
record.setMessage(this.prefix + record.getMessage());
// Don't trust loggers to show anything below INFO.
// Check the level ourselves and then promote the record
// to make sure it gets through.
if(record.getLevel().intValue() < Level.INFO.intValue() &&
record.getLevel().intValue() >= getEffectiveLevel().intValue()) {
record.setLevel(Level.INFO);
}
super.log(record);
}
/**
* Ensures that resource values are loaded. If they are not, loads them immediately.
*
* @param key key for the requested resource, or {@code null} if all resources
* are requested. This key is used mostly for constructing messages.
* @return the resources.
* @throws MissingResourceException if this method failed to load resources.
*/
private String[] ensureLoaded(final String key) throws MissingResourceException {
String[] values = this.values;
if (values == null) synchronized (this) {
values = this.values;
if (values == null) {
/*
* If there is no explicit resources for this instance, inherit the resources
* from the parent. Note that this IndexedResourceBundle instance may still
* differ from its parent in the way dates and numbers are formatted.
*/
if (resources == null) {
/*
* If we get a NullPointerException or ClassCastException here,
* it would be a bug in the way we create the chain of parents.
*/
values = ((IndexedResourceBundle) parent).ensureLoaded(key);
} else {
/*
* Prepares a log record. We will wait for successful loading before
* posting this record. If loading fails, the record will be changed
* into an error record. Note that the message must be logged outside
* the synchronized block, otherwise there is dead locks!
*/
final Locale locale = getLocale(); // Sometime null with IBM's JDK.
final String baseName = getClass().getCanonicalName();
final String methodName = (key != null) ? "getObject" : "getKeys";
final LogRecord record = new LogRecord(Level.FINER, "Loaded resources for {0} from bundle \"{1}\".");
record.setLoggerName(Loggers.LOCALIZATION);
/*
* Loads resources from the UTF file.
*/
try (DataInputStream input = new DataInputStream(new BufferedInputStream(resources.openStream()))) {
values = new String[input.readInt()];
for (int i=0; i<values.length; i++) {
values[i] = input.readUTF();
if (values[i].isEmpty()) {
values[i] = null;
}
}
} catch (IOException exception) {
record.setLevel (Level.WARNING);
record.setMessage(exception.getMessage()); // For administrator, use system locale.
record.setThrown (exception);
Logging.log(IndexedResourceBundle.class, methodName, record);
throw (MissingResourceException) new MissingResourceException(
Exceptions.getLocalizedMessage(exception, locale), // For users, use requested locale.
baseName, key).initCause(exception);
}
/*
* Now, logs the message. This message is provided only in English.
* Note that Locale.getDisplayName() may return different string on
* different Java implementation, but it doesn't matter here since
* we use the result only for logging purpose.
*/
String language = null;
if (locale != null) {
language = locale.getDisplayName(Locale.US);
}
if (language == null || language.isEmpty()) {
language = "<root>";
}
record.setParameters(new String[] {language, baseName});
Logging.log(IndexedResourceBundle.class, methodName, record);
resources = null; // Not needed anymore, let GC do its job.
}
this.values = values;
}
}
return values;
}