如何防止logback / slf4j解析新的行字符 [英] How to prevent logback/slf4j from parsing a new line character

查看:100
本文介绍了如何防止logback / slf4j解析新的行字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用SLF4j的logback登录我的应用程序。我有一个包含新行字符的字符串。它是字符串值的一部分,但不表示新行。当我打印字符串时,logback将其打印在一个新行中。如何防止这种情况?

I'm using logback with SLF4j for logging in my application. I have a string that contains a new line character. It is part of the string value but doesn't signify a new line. When I print the string, logback prints it in a new line. How to prevent this ?

代码:

String str = "george\nmason"
logger.info(str);

回归模式:

<pattern>[%d{dd MMM yyyy HH:mm:ss,SSS}] [%5p] [%X{sid}] [%-20C{0} %25M]:[%-4L] - %m%n</pattern>

预期:

[19 Feb 2015 20:19:27] [ INFO] [] [myClass myMethod]:[52  ] - george\nmason

实际产出:

[19 Feb 2015 20:19:27] [ INFO] [] [myClass myMethod]:[52  ] - george
mason


推荐答案

您可以添加 替换 您的模式的选项,以便用其他内容替换邮件中的任何换行符,例如空格:

You could add a replace option to your pattern in order to replace any new-line characters from the message with something else, such as a space:

%replace(%m){'\n', ' '}

在你的你需要用 \\ n 替换 \ n

<pattern>[%d{dd MMM yyyy HH:mm:ss,SSS}] [%5p] [%X{sid}] [%-20C{0} %25M]:[%-4L] - %replace(%m){'\n', '\\n'}%n</pattern>

这篇关于如何防止logback / slf4j解析新的行字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆