Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[LOGMGR-245] Formatter wrapping #242

Merged
merged 1 commit into from
Mar 20, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions core/src/main/java/org/jboss/logmanager/ExtFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}