Localize and format the message string from a log record. This
method is provided as a convenience for Formatter subclasses to
use when they are performing formatting.
The message string is first localized to a format string using
the record's ResourceBundle. (If there is no ResourceBundle,
or if the message key is not found, then the key is used as the
format string.) The format String uses java.text style
formatting.
- If there are no parameters, no formatter is used.
- Otherwise, if the string contains "{0" then
java.text.MessageFormat is used to format the string.
- Otherwise no formatting is performed.
String format = record.getMessage();
java.util.ResourceBundle catalog = record.getResourceBundle();
if (catalog != null) {
// // We cache catalog lookups. This is mostly to avoid the
// // cost of exceptions for keys that are not in the catalog.
// if (catalogCache == null) {
// catalogCache = new HashMap();
// }
// format = (String)catalogCache.get(record.essage);
// if (format == null) {
try {
format = catalog.getString(record.getMessage());
} catch (java.util.MissingResourceException ex) {
// Drop through. Use record message as format
format = record.getMessage();
}
// catalogCache.put(record.message, format);
// }
}
// Do the formatting.
try {
Object parameters[] = record.getParameters();
if (parameters == null || parameters.length == 0) {
// No parameters. Just return format string.
return format;
}
// Is is a java.text style format?
// Ideally we could match with
// Pattern.compile("\\{\\d").matcher(format).find())
// However the cost is 14% higher, so we cheaply check for
// 1 of the first 4 parameters
if (format.indexOf("{0") >= 0 || format.indexOf("{1") >=0 ||
format.indexOf("{2") >=0|| format.indexOf("{3") >=0) {
return java.text.MessageFormat.format(format, parameters);
}
return format;
} catch (Exception ex) {
// Formatting failed: use localized format string.
return format;
}