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

Color log support #49

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@

package org.jboss.logmanager;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

Expand All @@ -35,10 +38,40 @@ public final String format(final LogRecord record) {
/**
* Format a message using an extended log record.
*
* @param extLogRecord the log record
* @param record the log record
* @return the formatted message
*/
public abstract String format(final ExtLogRecord extLogRecord);
public abstract String format(ExtLogRecord record);

@Override
public String formatMessage(LogRecord record) {
final ResourceBundle bundle = record.getResourceBundle();
String msg = record.getMessage();
if (msg == null) {
return null;
}
if (bundle != null) {
try {
msg = bundle.getString(msg);
} catch (MissingResourceException ex) {
// ignore
}
}
final Object[] parameters = record.getParameters();
if (parameters == null || parameters.length == 0) {
return formatMessageNone(record);
}
if (record instanceof ExtLogRecord) {
final ExtLogRecord extLogRecord = (ExtLogRecord) record;
final ExtLogRecord.FormatStyle formatStyle = extLogRecord.getFormatStyle();
if (formatStyle == ExtLogRecord.FormatStyle.PRINTF) {
return formatMessagePrintf(record);
} else if (formatStyle == ExtLogRecord.FormatStyle.NO_FORMAT) {
return formatMessageNone(record);
}
}
return msg.indexOf('{') >= 0 ? formatMessageLegacy(record) : formatMessageNone(record);
}

/**
* Determines whether or not this formatter will require caller, source level, information when a log record is
Expand All @@ -54,4 +87,37 @@ public final String format(final LogRecord record) {
public boolean isCallerCalculationRequired() {
return true;
}
}

/**
* Format the message text as if there are no parameters. The default implementation delegates to
* {@link LogRecord#getMessage() record.getMessage()}.
*
* @param record the record to format
* @return the formatted string
*/
protected String formatMessageNone(LogRecord record) {
return record.getMessage();
}

/**
* Format the message text as if there are no parameters. The default implementation delegates to
* {@link MessageFormat#format(String, Object[]) MessageFormat.format(record.getMessage(),record.getParameters())}.
*
* @param record the record to format
* @return the formatted string
*/
protected String formatMessageLegacy(LogRecord record) {
return MessageFormat.format(record.getMessage(), record.getParameters());
}

/**
* Format the message text as if there are no parameters. The default implementation delegates to
* {@link String#format(String, Object[]) String.format(record.getMessage(),record.getParameters())}.
*
* @param record the record to format
* @return the formatted string
*/
protected String formatMessagePrintf(LogRecord record) {
return String.format(record.getMessage(), record.getParameters());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public abstract class ExtHandler extends Handler implements FlushableCloseable {
* The sub-handlers for this handler. May only be updated using the {@link #handlersUpdater} atomic updater. The array
* instance should not be modified (treat as immutable).
*/
@SuppressWarnings({ "UnusedDeclaration" })
@SuppressWarnings("unused")
protected volatile Handler[] handlers;

/**
Expand Down Expand Up @@ -296,9 +296,12 @@ public boolean isCallerCalculationRequired() {
Formatter formatter = getFormatter();
if (formatterRequiresCallerCalculation(formatter)) {
return true;
} else {
final Handler[] handlers = getHandlers();
for (Handler handler : handlers) {
} else for (Handler handler : getHandlers()) {
if (handler instanceof ExtHandler) {
if (((ExtHandler) handler).isCallerCalculationRequired()) {
return true;
}
} else {
formatter = handler.getFormatter();
if (formatterRequiresCallerCalculation(formatter)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ public ExtLogRecord(final ExtLogRecord original) {
ndc = original.ndc;
loggerClassName = original.loggerClassName;
threadName = original.threadName;
resourceKey = original.resourceKey;
formattedMessage = original.formattedMessage;
hostName = original.hostName;
processName = original.processName;
processId = original.processId;
Expand Down Expand Up @@ -150,8 +148,6 @@ public static ExtLogRecord wrap(LogRecord rec) {
private FastCopyHashMap<String, Object> mdcCopy;
private int sourceLineNumber = -1;
private String sourceFileName;
private String resourceKey;
private String formattedMessage;
private String threadName;
private String hostName;
private String processName;
Expand All @@ -172,8 +168,6 @@ private void readObject(ObjectInputStream ois) throws IOException, ClassNotFound
mdcCopy = (FastCopyHashMap<String, Object>) fields.get("mdcCopy", new FastCopyHashMap<>());
sourceLineNumber = fields.get("sourceLineNumber", -1);
sourceFileName = (String) fields.get("sourceFileName", null);
resourceKey = (String) fields.get("resourceKey", null);
formattedMessage = (String) fields.get("formattedMessage", null);
threadName = (String) fields.get("threadName", null);
hostName = (String) fields.get("hostName", null);
processName = (String) fields.get("processName", null);
Expand All @@ -199,7 +193,6 @@ public void disableCallerCalculation() {
public void copyAll() {
copyMdc();
calculateCaller();
getFormattedMessage();
}

/**
Expand Down Expand Up @@ -462,36 +455,17 @@ public void setSourceModuleVersion(final String sourceModuleVersion) {
* Get the fully formatted log record, with resources resolved and parameters applied.
*
* @return the formatted log record
* @deprecated The formatter should normally be used to format the message contents.
*/
@Deprecated
public String getFormattedMessage() {
if (formattedMessage == null) {
formattedMessage = formatRecord();
}
return formattedMessage;
}

/**
* Get the resource key, if any. If the log message is not localized, then the key is {@code null}.
*
* @return the resource key
*/
public String getResourceKey() {
if (formattedMessage == null) {
formatRecord();
}
return resourceKey;
}

private String formatRecord() {
final ResourceBundle bundle = getResourceBundle();
String msg = getMessage();
if (msg == null)
return null;
if (bundle != null) {
try {
String locMsg = bundle.getString(msg);
resourceKey = msg;
msg = locMsg;
msg = bundle.getString(msg);
} catch (MissingResourceException ex) {
// ignore
}
Expand All @@ -512,6 +486,18 @@ private String formatRecord() {
return msg;
}

/**
* Get the resource key, if any. If the log message is not localized, then the key is {@code null}.
*
* @return the resource key
*/
public String getResourceKey() {
final String msg = getMessage();
if (msg == null) return null;
if (getResourceBundleName() == null && getResourceBundle() == null) return null;
return msg;
}

/**
* Get the thread name of this logging event.
*
Expand Down Expand Up @@ -603,7 +589,6 @@ public void setMessage(final String message) {
*/
public void setMessage(final String message, final FormatStyle formatStyle) {
this.formatStyle = formatStyle == null ? FormatStyle.MESSAGE_FORMAT : formatStyle;
formattedMessage = null;
super.setMessage(message);
}

Expand All @@ -613,7 +598,6 @@ public void setMessage(final String message, final FormatStyle formatStyle) {
* @param parameters the log message parameters. (may be null)
*/
public void setParameters(final Object[] parameters) {
formattedMessage = null;
super.setParameters(parameters);
}

Expand All @@ -623,7 +607,6 @@ public void setParameters(final Object[] parameters) {
* @param bundle localization bundle (may be null)
*/
public void setResourceBundle(final ResourceBundle bundle) {
formattedMessage = null;
super.setResourceBundle(bundle);
}

Expand All @@ -633,7 +616,6 @@ public void setResourceBundle(final ResourceBundle bundle) {
* @param name localization bundle name (may be null)
*/
public void setResourceBundleName(final String name) {
formattedMessage = null;
super.setResourceBundleName(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import java.util.logging.Filter;

import org.jboss.logmanager.ExtLogRecord;

/**
* A regular-expression-based filter. Used to exclude log records which match or don't match the expression. The
* regular expression is checked against the raw (unformatted) message.
Expand Down Expand Up @@ -59,12 +57,6 @@ public RegexFilter(final String patternString) {
*/
@Override
public boolean isLoggable(final LogRecord record) {
final String msg;
if (record instanceof ExtLogRecord) {
msg = ((ExtLogRecord) record).getFormattedMessage();
} else {
msg = record.getMessage();
}
return pattern.matcher(String.valueOf(msg)).find();
return record != null && pattern.matcher(String.valueOf(record.getMessage())).find();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.jboss.logmanager.filters;

import java.text.MessageFormat;
import java.util.logging.LogRecord;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -77,7 +78,7 @@ public boolean isLoggable(final LogRecord record) {
if (record instanceof ExtLogRecord) {
currentMsg = ((ExtLogRecord) record).getFormattedMessage();
} else {
currentMsg = record.getMessage();
currentMsg = MessageFormat.format(record.getMessage(), record.getParameters());
}
final Matcher matcher = pattern.matcher(String.valueOf(currentMsg));
final String msg;
Expand Down
Loading