如何从日志消息log4j中删除或替换换行符 [英] How to remove or replace newline from log messages log4j
问题描述
我正在使用下面的转换模式
I am using below conversion pattern
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS Z} [%t] %-5p %c- %m%n
但是我想删除换行符.
我尝试使用%replace(%ex){'[\ r \ n] +','\\ n'}%nopex%n
但是它不起作用%replace不起作用.它只会读取%r ,然后替换
but it's not working %replace is not working. it only reading %r and then eplace
推荐答案
It seems you were not using the right syntax of the %replace
- see Log4j Layouts for the right one.
使用正确的语法,您可以使用以下表达式删除所有换行符:
Using the right syntax, you could remove all the newlines using the following expression:
%replace{%msg}{[\r\n]+}{}
但是,在某些情况下,完全删除换行符可能会使日志难以阅读.我建议,不要删除,而是用一些东西替换换行符,例如带有这样的箭头字符:↵(我为与 Enter键)上的符号相似.
However, removing newlines altogether can make the log hard to read in certain cases. I would suggest that, instead of removing, you replace the newlines with something, e.g. with such an arrow character: ↵ (I chose it for its similarity with the symbol on the Enter key).
所以我建议使用这种模式:
So I would suggest using such a pattern:
%replace{%msg}{\r?\n}{↵}
这篇关于如何从日志消息log4j中删除或替换换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!