Search code examples
log4j

Limit message length in Log4J


I'm working on an application that uses Log4J for logging. Occasionally, the application creates log messages that are very long, > 10000 characters. This causes performance problems when the log is displayed in a Console window in Eclipse.

I want to limit the maximum message length, truncating long messages to the maximum length. How can I do that with Log4J?


Solution

  • You can use a PatternLayout with a ConversionPattern that has a 'maximum width' format modifier on the message conversion character.

    Example:

    %r [%t] %-5p %c %x - %.10000m%n
    

    limits message size to 10000 characters.