From 61fc3f40b8c9059f99352234f31e193992edefe8 Mon Sep 17 00:00:00 2001 From: "David M. Lloyd" Date: Fri, 7 Dec 2018 16:49:16 -0600 Subject: [PATCH] [LOGMGR-245] Formatter wrapping --- .../org/jboss/logmanager/ExtFormatter.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/core/src/main/java/org/jboss/logmanager/ExtFormatter.java b/core/src/main/java/org/jboss/logmanager/ExtFormatter.java index 6de80b0e..56b40832 100644 --- a/core/src/main/java/org/jboss/logmanager/ExtFormatter.java +++ b/core/src/main/java/org/jboss/logmanager/ExtFormatter.java @@ -23,12 +23,34 @@ import java.util.MissingResourceException; import java.util.ResourceBundle; import java.util.logging.Formatter; +import java.util.logging.Handler; import java.util.logging.LogRecord; /** * A formatter which handles {@link org.jboss.logmanager.ExtLogRecord ExtLogRecord} instances. */ public abstract class ExtFormatter extends Formatter { + /** + * Construct a new instance. + */ + public ExtFormatter() { + } + + /** + * Wrap an existing formatter with an {@link ExtFormatter}, optionally replacing message formatting with + * the default extended message formatting capability. + * + * @param formatter the formatter to wrap (must not be {@code null}) + * @param formatMessages {@code true} to replace message formatting, {@code false} to let the original formatter do it + * @return the extended formatter (not {@code null}) + */ + public static ExtFormatter wrap(Formatter formatter, boolean formatMessages) { + if (formatter instanceof ExtFormatter && ! formatMessages) { + return (ExtFormatter) formatter; + } else { + return new WrappedFormatter(formatter, formatMessages); + } + } /** {@inheritDoc} */ public final String format(final LogRecord record) { @@ -120,4 +142,34 @@ protected String formatMessageLegacy(LogRecord record) { protected String formatMessagePrintf(LogRecord record) { return String.format(record.getMessage(), record.getParameters()); } + + static class WrappedFormatter extends ExtFormatter { + private final Formatter formatter; + private final boolean formatMessages; + + WrappedFormatter(Formatter formatter, boolean formatMessages) { + this.formatter = formatter; + this.formatMessages = formatMessages; + } + + @Override + public String format(ExtLogRecord record) { + return formatter.format(record); + } + + @Override + public String formatMessage(LogRecord record) { + return formatMessages ? super.formatMessage(record) : formatter.formatMessage(record); + } + + @Override + public String getHead(Handler h) { + return formatter.getHead(h); + } + + @Override + public String getTail(Handler h) { + return formatter.getTail(h); + } + } } \ No newline at end of file