如果邮件中不包含单词

如果邮件中不包含单词

本文介绍了Logback模式-如果邮件中不包含单词,请进行替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是

我使用以下模式来避免\ n的字符串换成新行.但是在使用了这种模式之后,我的堆栈跟踪在开头就出现了\ n,这看起来很尴尬.

I use following pattern to avoid strings with \n breaking into new line. But after using this pattern, my stacktraces come up with \n in the beginning which looks awkward.

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

结果堆栈跟踪:

[21 Feb 2015 23:14:24] [ERROR] [21181422764] [myclass mymethod]:[221] - Socket exception occured while sending request for user. Stacktrace:= java.net.ConnectException: Connection refused: connect
\n  at java.net.DualStackPlainSocketImpl.connect0(Native Method)

我必须更改替换,以仅匹配其中没有"Exception"字样的%m.不确定如何以模式完成此操作.另外,我担心整体匹配和替换会增加日志记录时间成本以及可接受的成本.

I've to change the replace to match only those %m that do not have "Exception" word in them. Not sure how to accomplish this in pattern. Also, I'm concerned about how this overall match and replace is adding to logging time cost and if it is acceptable cost.

推荐答案

您似乎正在手动将异常格式设置为消息的一部分,但是如果您将异常作为日志的参数包括在内,则logback会为您执行此操作调用(请参见 SLF4J API ).我建议使用该API,而不是尝试将其包含在您的消息中.

It looks like you're manually formatting the exception as part of your message, but logback does this for you if you include the exception as an argument of the log call (see SLF4J API). I recommend using that API instead of attempting to include it in your message.

final Logger log = LoggerFactory.getLogger(Foo.class);
try {
    // ...
} catch (IOException e) {
    log.error("Socket exception occurred while sending request for user.", e);
}

如果您担心性能,请注意,默认情况下,注销包含包装信息用于堆栈跟踪中的每个方法,这可能会很昂贵,尤其是在您的应用程序频繁记录异常的情况下.要跳过打包信息查找,请将 %ex 附加到您的模式布局.

If you're concerned with performance, note that by default, logback includes packaging information for each method in the stacktrace, which could be expensive, especially if your application frequently logs exceptions. To skip the package-info lookup, append %ex to your pattern layout.

在您的示例中:

<pattern>... %replace(%m){'\n', '\\n'}%n%ex</pattern>

这篇关于Logback模式-如果邮件中不包含单词,请进行替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:46