How to remove or replace newline from log messages log4j

百般思念 提交于 2020-06-13 08:26:29

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!