问题
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
but I want to remove new line characters.
I have tried using %replace(%ex){'[\r\n]+', '\\n'}%nopex%n
but it's not working %replace is not working. it only reading %r and then eplace
回答1:
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]+}{}
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}{↵}
回答2:
Alternative to @Tomasz Linkowski suggestion is to add the new line at the end
%replace{%msg}{[\r\n]}{}%n
来源:https://stackoverflow.com/questions/51466025/how-to-remove-or-replace-newline-from-log-messages-log4j