[logback-user] Custom formatting of parameterized string

david_z dzeigler at gmail.com
Thu Sep 10 04:04:03 CEST 2009

RossJ wrote:
> Hello,
> I would like to be able to use parameterized logging with a byte[]
> argument (formatted as a hex string). Obviously this doesn't work today
> because arrays don't have a toString() implementation.
> One way I can see to do this is to modify (or substitute) the
> ch.qos.logback.classic.pattern.MessageConverter class and base my
> implementation on the org.slf4j.helpers.MessageFormatter class. I can
> either build my own logback.jar or manipulate the classpath to make it
> active.
> Is there a better way to do this? Is customization of the parameterized
> formatting an area that is likely to be enhanced in future releases? Or is
> my requirement somehow unusual...
> /Thanks, Ross.

I'm new to logback and am in the process of evaluating as a replacement for
log4j.  A question was raised today at work regarding parameterized logging
and the ability to modify logback to support java.text.MessageFormat style
formatting in order to, for example, perform date or currency formatting: 
log.debug("myDate={0,date,MMM d}", myDate);
log.debug("myAmount={0,number,currency}", myAmount);

or to defer array formatting, similar to the question I'm replying to above.

I appreciate the performance reasons for logback using {} replacement, but
it seems like it should be possible to allow the user to provide an
alternate formatting strategy if they so desire without negatively impacting
the default's performance.  Creating my own MessageConverter, as suggested
above, to perform this formatting works fine, except the conversion is
performed once per appender per event rather than just once per event.

Is there a better way to handle this custom formatting goal?

If not, I have a suggestion for an enhancement.  
1. Add a Formatter interface with the method
   String format(String messagePattern, Object[] argArray)  
2. The LoggingEvent would need access to the formatter.  Ideally the
Formatter could be configured via the config file as the Converters can be. 
I don't know enough about the source to flesh this part out well.
3. Modify getFormattedMessage() in the ILoggingEvent implementations
  public String getFormattedMessage() {
    if (formattedMessage != null) {
      return formattedMessage;
    if (argumentArray != null) {
      if (formatter == null) {
        formattedMessage = MessageFormatter.arrayFormat(message,
      } else {
        formattedMessage = formatter.format(message, argumentArray);
    } else {
      formattedMessage = message;
    return formattedMessage;
What do you think?


View this message in context: http://www.nabble.com/Custom-formatting-of-parameterized-string-tp13972167p25375735.html
Sent from the Logback User mailing list archive at Nabble.com.

More information about the Logback-user mailing list