在Log4J中限制消息的长度[英] Limit message length in Log4J

本文是小编为大家收集整理的关于在Log4J中限制消息的长度的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在研究使用log4j进行记录的应用程序.有时,该应用程序会创建非常长的日志消息,> 10000个字符.当日志显示在Eclipse的控制台窗口中时,这会导致性能问题.

我想限制最大消息长度,将长消息截断至最大长度.我该如何使用log4j?

推荐答案

您可以使用带有在消息转换字符上具有"最大宽度"格式修饰符的转换模式.

示例:

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

将消息大小限制为10000个字符.

其他推荐答案

如果有人正在寻找最大宽度,那是删除最后一个字符,而不是第一个(就像我一样),这是一个示例:

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

将消息大小限制为10000个字符,并删除末尾的所有内容.

来源: -926

本文地址:https://www.itbaoku.cn/post/1574839.html

问题描述

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?

推荐答案

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.

其他推荐答案

If someone is looking for a maximum width, that is removing the last characters and not the first (like i did) here is an example:

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

limits the message size to 10000 characters and removes everything on the end that is longer.

source: https://issues.apache.org/jira/browse/LOG4J2-926