如何从Logback中的Java堆栈跟踪中删除换行符? [英] How do I remove newline from Java stacktraces in logback?
问题描述
我正在尝试从Java堆栈跟踪中删除换行符.
I'm trying to remove newline from Java stacktraces.
我遵循的是logback模式-
I've following logback pattern -
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %replace(%msg){'\n', ''}%n</pattern>
我希望它能替换邮件中的换行符,但它不会这样做.我看到用换行符打印的堆栈跟踪.
I expect it to replace newlines in messages but it is not doing so. I see stacktraces printed out with newlines.
但是,如果我使用以下模式(仅用于测试目的)-
However, if I use following pattern (for testing purpose only)-
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %replace(%msg){'.*', 'x'}%n</pattern>
我发现消息已被替换为字符x,但是堆栈跟踪仍按原样打印.
I find that messages are getting replaced with character x but stack traces are still getting printed as is.
这使我相信logback对栈跟踪的处理是独立的. 我浏览了logback文档,该文档指出可以通过(%ex)引用堆栈交易记录.
This makes me believe that logback treats stacktraces indepdentantly. I've gone through the logback documentation it says that one can refer to stacktraes via (%ex).
但是,随后我只能使追加程序一次处于活动状态. 如何确保在堆栈跟踪中没有换行符?
But then, I can have only one pattern active at a time for the appender. How do I go about making sure that I don't get newline in stacktraces?
推荐答案
查看Logback格式化程序如何在Windows框上构建字符串后,我看到堆栈跟踪的每一行都包含\r\n
,因此如果使用Windows,请尝试以下方法:
After looking at how Logback formatter builds the string on my Windows box, I saw that the stack trace contains \r\n
for each new line, so if you are using Windows, try this instead:
<pattern>........ %replace(%ex){'[\r\n]+', '\\n'}%nopex%n</pattern>
说明:
-
\r\n
:起初我只尝试使用%replace(%ex){'\n', 'X'}
,但随后在日志中输入了换行符,然后是'X'.相反,使用%replace(%ex){'[\r\n]+', ''}
会删除所有出现的\r
和\n
.如果要显示字符串"\n"
表示此处有新行,则可以在%replace
第二个参数中添加'\\n'
.
\r\n
: At first I tried with only%replace(%ex){'\n', 'X'}
but then I had in the logs the new line character then a 'X'. Instead, using%replace(%ex){'[\r\n]+', ''}
removes all occurrences of\r
and\n
. If you want to display the string"\n"
to indicate that a new line was here, you can add'\\n'
in the%replace
second parameter.
%nopex
:我不知道为什么,但是如果您在%ex
上使用%replace
,则Logback将第二个堆栈跟踪添加到您的条目中,这次包含所有新行.使用%nopex
(请参阅文档),它将通过伪造来抑制第二个堆栈使用没有任何%replace
的实际堆栈.
%nopex
: I don't know why, but if you use %replace
on %ex
, Logback adds a second stack trace to your entry, this time with all the new lines. Using %nopex
(see documentation), it will suppress this second stack by faking the use of an actual stack without any %replace
.
这篇关于如何从Logback中的Java堆栈跟踪中删除换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!