Logback string length

Viewed 2002

I'm using logback to log to a rolling file in my Spring Boot service. In one of my classes, I am attempting to log a really long string (a string value of a very large object array that can have tens of thousands of elements).

log.info(str);

The above does not print, probably because the value of str is a very very long string.

//logback truncates the list - to preserve this we should record it in an audit table
            String rl = String.valueOf(reptIdList);
            String truncRlStr = rl.substring(0, Math.min(rl.length(), 100));
        log.info(truncRlStr);

The above does print. Probably because I'm truncating the very long string to 100 characters.

There is probably a limit to the length of the string in logback - is there a way to set it to maximum string length? I notice that there is no limitation on string length when logging into the console.

Thanks for any advice.

1 Answers

I know it is an old question now and OS moved on in her/his life, but it would be useful to someone coming to this thread.

You can limit the number of characters logback would log using: %.-<limit>msg

e.g. to limit the number of characters to 100:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
        <Pattern>
            %.-30msg%n
        </Pattern>
    </layout>
</appender>

PS: I tested this using ch.qos.logback library

Related