Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-950

conversionRule to have ability to customize MessageFormatter

    XMLWordPrintable

Details

    • Icon: Improvement Improvement
    • Resolution: Unresolved
    • Icon: Minor Minor
    • None
    • None
    • logback-classic
    • None

    Description

      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
      
      

      Attachments

        Activity

          People

            logback-dev Logback dev list
            vlads Vlad Skarzhevskyy
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated: