下面列出了java.util.logging.LogRecord#setMessage ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void publish(LogRecord record) {
if (shouldRotate(record)) {
synchronized (monitor) {
if (shouldRotate(record)) {
rotateDate();
}
}
}
if (System.currentTimeMillis() - startDate > 25 * 60 * 60 * 1000) {
String msg = record.getMessage();
record.setMessage("missed file rolling at: " + new Date(endDate) + "\n" + msg);
}
handler.publish(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() >= this.getLevel().intValue()) {
record.setLevel(Level.INFO);
}
super.log(record);
}
@Test
public void assertTestFormatting() throws IOException {
Level level = Level.SEVERE;
long recordMillis = 874400705000L;
String recordSourceClassName = "org.my.test.Class";
String recordSourceMethodName = "mySourceMethod";
String recordMessage = "Formatting Log Message";
Throwable throwable = new Throwable() {
public void printStackTrace(PrintWriter s) {
StackTraceElement[] trace = getStackTrace();
for (StackTraceElement traceElement : trace) {
s.println("\tat " + traceElement);
}
}
};
StackTraceElement recordStackTrace1 = new StackTraceElement("declaringClass", "methodName", "fileName.java", 1);
StackTraceElement recordStackTrace2 = new StackTraceElement("declaringClass2", "methodName2.drl.java",
"fileName2.java", 2);
StackTraceElement recordStackTrace3 = new StackTraceElement("declaringClass", "methodName.apk.java", "fileName",
3);
StackTraceElement[] recordStackTrace = { recordStackTrace1, recordStackTrace2, recordStackTrace3 };
throwable.setStackTrace(recordStackTrace);
LogRecord record = new LogRecord(level, recordMessage);
record.setMillis(recordMillis);
record.setSourceClassName(recordSourceClassName);
record.setSourceMethodName(recordSourceMethodName);
record.setMessage(recordMessage);
record.setThrown(throwable);
String expectedOutput = "Sep 16, 1997 09:05:05 org.my.test.Class mySourceMethod()" + lineSeparator()
+ "Message: Formatting Log Message" + lineSeparator()
+ "\tat declaringClass.methodName(fileName.java:1)" + lineSeparator()
+ "\tat declaringClass2.methodName2.drl.java(fileName2.java:2)" + lineSeparator()
+ "\tat declaringClass.methodName.apk.java(fileName:3)" + lineSeparator();
assertEquals(expectedOutput, LSPClientLogHandler.formatRecord(record, Locale.US));
}
@Override
public void publish(LogRecord record) {
if (shouldRotate(record)) {
synchronized (monitor) {
if (shouldRotate(record)) {
rotateDate();
}
}
}
if (System.currentTimeMillis() - startDate > 25 * 60 * 60 * 1000) {
String msg = record.getMessage();
record.setMessage("missed file rolling at: " + new Date(endDate) + "\n" + msg);
}
handler.publish(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);
}
@Override
public boolean isLoggable(final LogRecord record) {
if (isLoggable) {
final StringBuilder newMsg = new StringBuilder(ExtLogRecord.wrap(record).getFormattedMessage());
if (constructorText != null) {
newMsg.append(constructorText);
}
if (propertyText != null) {
newMsg.append(propertyText);
}
record.setMessage(newMsg.toString());
}
return isLoggable;
}
@Override
public boolean isLoggable(final LogRecord record) {
if (isLoggable) {
final StringBuilder newMsg = new StringBuilder(ExtLogRecord.wrap(record).getFormattedMessage());
if (constructorText != null) {
newMsg.append(constructorText);
}
if (propertyText != null) {
newMsg.append(propertyText);
}
record.setMessage(newMsg.toString());
}
return isLoggable;
}
@Override
public void log(LogRecord record) {
if (loggerPrefix == null) {
loggerPrefix = "[" + properties.getName() + "]";
}
record.setMessage("[" + heavySpleef.getPlugin().getName() + "] " + loggerPrefix + " " + record.getMessage());
super.log(record);
}
/**
* Tests formatting a log record which contains an exception.
*/
@Test
@DependsOnMethod("testlevelWidth")
public void testException() {
final LogRecord record = new LogRecord(Level.WARNING, "An exception occured.");
final Exception exception = new Exception();
exception.setStackTrace(new StackTraceElement[] {
new StackTraceElement("org.apache.sis.NonExistent", "foo", "NonExistent.java", 10),
new StackTraceElement("org.junit.WhoKnows", "main", "WhoKnows.java", 20)
});
record.setThrown(exception);
String formatted = formatter.format(record);
assertMultilinesEquals(localize(Level.WARNING,
"WARNING\t An exception occured.\n" +
"\t Caused by: java.lang.Exception\n" +
"\t at org.apache.sis.NonExistent.foo(NonExistent.java:10)\n" +
"\t at org.junit.WhoKnows.main(WhoKnows.java:20)\n"), formatted);
/*
* Remove the message and try again.
*/
record.setMessage(null);
formatted = formatter.format(record);
assertMultilinesEquals(localize(Level.WARNING,
"WARNING\t java.lang.Exception\n" +
"\t at org.apache.sis.NonExistent.foo(NonExistent.java:10)\n" +
"\t at org.junit.WhoKnows.main(WhoKnows.java:20)\n"), formatted);
}
@Override
public boolean isLoggable(LogRecord record) {
if (record == null || record.getMessage() == null) {
return true;
}
if (LogFilterHelper.isSensitiveAuthMeCommand(record.getMessage())) {
String playerName = record.getMessage().split(" ")[0];
record.setMessage(playerName + " issued an AuthMe command");
}
return true;
}
@Override
public void log(LogRecord logRecord) {
logRecord.setMessage(pluginName + logRecord.getMessage());
super.log(logRecord);
}
/** Calls all delegates. */
public Throwable annotate(
Throwable t, int severity, String message, final String localizedMessage, Throwable stackTrace,
java.util.Date date
) {
if (delegates.isEmpty()) {
LogRecord rec = new LogRecord(convertSeverity(severity, true, Level.ALL), message);
if (stackTrace != null) {
rec.setThrown(stackTrace);
}
if (date != null) {
rec.setMillis(date.getTime());
}
if (localizedMessage != null) {
ResourceBundle rb = new ResourceBundle() {
public Object handleGetObject(String key) {
if ("msg".equals(key)) { // NOI18N
return localizedMessage;
} else {
return null;
}
}
public Enumeration<String> getKeys() {
return Enumerations.singleton("msg"); // NOI18N
}
};
rec.setResourceBundle(rb);
rec.setMessage("msg"); // NOI18N
}
AnnException ann = AnnException.findOrCreate(t, true);
if (ann != null) { //#148778 - Although ann should not be null, it was reported it can happen.
ann.addRecord(rec);
}
return t;
}
for (ErrorManager em : delegates) {
em.annotate(t, severity, message, localizedMessage, stackTrace, date);
}
return t;
}
@Override
public void publish(LogRecord record) {
record.setMessage("[" + (System.currentTimeMillis() - startTimeMillis) + " ms.] " + record.getMessage()); // NOI18N
}
@Override
public void log(LogRecord logRecord) {
logRecord.setMessage(this.prefix + logRecord.getMessage());
super.log(logRecord);
}
@Override
public void log(LogRecord record) {
record.setMessage("[PlotMe]");
super.log(record);
}
@Override
public void log(LogRecord logRecord) {
logRecord.setMessage(this.prefix + logRecord.getMessage());
super.log(logRecord);
}
/**
* 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;
}
private void colorize(LogRecord logrecord) {
// ORIGINAL CODE FROM org.bukkit.craftbukkit.command.ColouredConsoleSender
final Map<ChatColor, String> replacements = new EnumMap<>(ChatColor.class);
replacements
.put(ChatColor.BLACK, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).boldOff().toString());
replacements
.put(ChatColor.DARK_BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).boldOff().toString());
replacements.put(ChatColor.DARK_GREEN,
Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).boldOff().toString());
replacements
.put(ChatColor.DARK_AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).boldOff().toString());
replacements
.put(ChatColor.DARK_RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).boldOff().toString());
replacements.put(ChatColor.DARK_PURPLE,
Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).boldOff().toString());
replacements
.put(ChatColor.GOLD, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).boldOff().toString());
replacements.put(ChatColor.GRAY, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).boldOff().toString());
replacements
.put(ChatColor.DARK_GRAY, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLACK).bold().toString());
replacements.put(ChatColor.BLUE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.BLUE).bold().toString());
replacements.put(ChatColor.GREEN, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.GREEN).bold().toString());
replacements.put(ChatColor.AQUA, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.CYAN).bold().toString());
replacements.put(ChatColor.RED, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.RED).bold().toString());
replacements.put(ChatColor.LIGHT_PURPLE,
Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.MAGENTA).bold().toString());
replacements.put(ChatColor.YELLOW, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.YELLOW).bold().toString());
replacements.put(ChatColor.WHITE, Ansi.ansi().a(Ansi.Attribute.RESET).fg(Ansi.Color.WHITE).bold().toString());
replacements.put(ChatColor.MAGIC, Ansi.ansi().a(Ansi.Attribute.BLINK_SLOW).toString());
replacements.put(ChatColor.BOLD, Ansi.ansi().a(Ansi.Attribute.UNDERLINE_DOUBLE).toString());
replacements.put(ChatColor.STRIKETHROUGH, Ansi.ansi().a(Ansi.Attribute.STRIKETHROUGH_ON).toString());
replacements.put(ChatColor.UNDERLINE, Ansi.ansi().a(Ansi.Attribute.UNDERLINE).toString());
replacements.put(ChatColor.ITALIC, Ansi.ansi().a(Ansi.Attribute.ITALIC).toString());
replacements.put(ChatColor.RESET, Ansi.ansi().a(Ansi.Attribute.RESET).toString());
String result = logrecord.getMessage();
for (ChatColor color : ChatColor.values()) {
if (replacements.containsKey(color)) {
result = result.replaceAll("(?i)" + color.toString(), replacements.get(color));
} else {
result = result.replaceAll("(?i)" + color.toString(), "");
}
}
result += Ansi.ansi().reset().toString();
logrecord.setMessage(result);
}
@Override
public void log(LogRecord logRecord) {
logRecord.setMessage(this.prefix + logRecord.getMessage());
super.log(logRecord);
}