The target was to use function other then toString() on object that are passed to MessageFormatter.
Reason is the toString() will be used by business presentation and debug presentation will be different.
Attempt had been made to customize org.slf4j.helpers.MessageFormatter just using logback API and configuration files.
Example implementation attached.
Short code summary:
<conversionRule conversionWord="message" converterClass="org.sample.DebugViewMessageConverter" />
<appender name="ConsoleAppender1" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<pattern>[2]%-5level %message\n</pattern>
</encoder>
</appender>
public interface IDebugView {
public String getDebugView();
}
public class DebugViewMessageConverter extends ClassicConverter {
@Override
public String convert(ILoggingEvent event) {
Object[] argumentArray = event.getArgumentArray();
if (argumentArray != null) {
boolean hasDebugView = false;
for (int i = 0; i < argumentArray.length; i++) {
if (argumentArray[i] instanceof IDebugView) {
argumentArray[i] = ((IDebugView) argumentArray[i]).getDebugView();
hasDebugView = true;
}
}
if (hasDebugView) {
// Need to store it in Event so multiple loggers will use this message, otherwise toString() of the objects will be used in message.
LoggingEventAccess.setFormattedMessage((LoggingEvent) event, MessageFormatter.arrayFormat(event.getMessage(), argumentArray).getMessage());
}
}
return event.getFormattedMessage();
}
}
public class DebugObject implements IDebugView {
private final String text;
public DebugObject(String text) {
this.text = text;
}
public String getDebugView() {
return text;
}
@Override
public String toString() {
return "!!not this!!";
}
}
log.info("test message: {}", new DebugObject("print this"));
result:
INFO test message: print this
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira