Limit message length in Log4J

2019-02-25 17:56发布

问题:

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?

回答1:

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.



标签: log4j