[logback-user] toString alternative in MessageFormatter

Kallin Nagelberg kallin.nagelberg at gmail.com
Mon Sep 8 07:27:54 CEST 2014

Hey everyone,

I'm interested in making a custom converter to replace the MessageConverter
that renders certain objects differently depending on configuration.

Right now I have something like:

public class MyMessageConverter extends MessageConverter {

    public String convert(ILoggingEvent event) {
        Object[] argumentArray = event.getArgumentArray();
        if (argumentArray != null) {
            for (Object o : argumentArray) {
                if (o.getClass() == BoardState.class) {
MyMessageFormatter.arrayFormat(event.getMessage(), argumentArray,
        return super.convert(event);

Within 'MyMessageFormatter', which I copied
from org.slf4j.helpers.MessageFormatter, I override String oAsString =
o.toString(); within 'safeObjectAppend', depending on the class and the
options. IE, I may want a simple string representation of a given object or
something verbose. Essentially, I want to choose methods on those objects
besides 'toString'.

It seems wrong to have to copy so much code, and would be great if
MessageConverter was more extensible and didn't depend on the difficult to
configure MessageFormatter. Am I going about this wrong?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.qos.ch/pipermail/logback-user/attachments/20140908/8a1d99f1/attachment.html>

More information about the Logback-user mailing list