[slf4j-dev] [JIRA] Updates for SLF4J-552: Disable pattern programatically in current thread.

QOS.CH (JIRA) noreply-jira at qos.ch
Tue Jun 28 21:28:00 CEST 2022


SLF4J / SLF4J-552 [Closed]
Disable pattern programatically in current thread.

==============================

Here's what changed in this issue in the last few minutes.

There is 1 comment.

View or comment on issue using this link
https://jira.qos.ch/browse/SLF4J-552

==============================
 1 comment
------------------------------

Martin on 28/Jun/22 9:13 PM
For other people that need this, below is my solution. This will create only one event until the MDC is removed.

This gives the possibility to make it dynamic within the code. I know, there is a risk to loose logs but this is only for development.

I was able to do it with Custom CompositeConverter and MDC.
{code:java}
public class CompactDebugLoggingConverter extends CompositeConverter<ILoggingEvent> implements CompactLoggingManager {
   private final StringBuilder buffer =new StringBuilder();

   @Override
   public String convert(ILoggingEvent event) {
      if (event.getMDCPropertyMap().containsKey(COMPACT_MSG) && event.getLevel().equals(Level.DEBUG) &&
            event.getThrowableProxy() == null) {
         boolean startBuffering = buffer.length() == 0;
         if(startBuffering){
            buffer.append(super.convert(event));
         } else {
            buffer.append(event.getFormattedMessage()).append('\n');
         }
         return "";
      } else {
         String message= buffer.append(super.convert(event)).toString();
         buffer.setLength(0);
         return message;
      }
   }

   @Override
   protected String transform(ILoggingEvent event, String in) {
      return in;
   }
}
{code}
{code:xml}
   <conversionRule conversionWord="compactMsg" converterClass="log.converter.CompactDebugLoggingConverter"/>
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%compactMsg(%d{yyyy-MM-dd HH:mm:ss.SSS} ${LOG_LEVEL_PATTERN:-%5p} --- [%8.8t] %-40.40logger{39} : %msg%n)
            </pattern>
            <charset>utf8</charset>
        </encoder>
    </appender>
{code}


==============================
 This message was sent by Atlassian Jira (v8.8.0#808000-sha1:e2c7e59)



More information about the slf4j-dev mailing list