Search code examples
springlogbackelastic-stack

Truncate individual log messages above a certain character count with Spring logback


I would like to reduce the size of certain logs using logback (I attempted to use Pattern layout with with a pattern of %.100000msg to limit the max size to one hundred thousand but had no luck), the large(over 1 million characters caused by a few REST GET calls) logs are causing Elastic to slow down when searching for certain information.

What would be the best practice to overcome this?

<include resource="org/springframework/boot/logging/logback/base.xml"/>
    <appender name="Console"
              class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %.30msg
            </Pattern>
        </layout>
    </appender>
    <logger name="" level="INFO"/>

Solution

  • I solved this by refering to this documentation to create a custom converter. The property was an extra, it was implemented to allow for configuration within the xml file.

    <property scope="context" name="max-message-length" value="100000"/>
    
    <conversionRule conversionWord="boundedMsg"
                        converterClass="za.co.ksdc.qadee.config.TruncationCustomLogConverter" />
    

    The following java class was implemented to truncate the log message:

    public class TruncationCustomLogConverter extends ClassicConverter {
    
        @Override
        public String convert(ILoggingEvent event) {
            int maxMessageLength = Integer.parseInt(getContext().getProperty("max- 
         message-length"));
            String formattedMessage = event.getFormattedMessage();
            if (formattedMessage == null ||
                    formattedMessage.length() < maxMessageLength) {
                return formattedMessage;
            }
            return new StringBuilder(maxMessageLength)
                    .append(formattedMessage, 0, maxMessageLength)
                    .toString();
        }
    }