如果登录中有异常,请添加一些要记录的内容 [英] Add something to log if there is an exception in logback

查看:91
本文介绍了如果登录中有异常,请添加一些要记录的内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我无法控制的原因,某些必须使用的日志记录基础结构无法正确处理换行符.

Due to reasons outside of my control, newlines aren't handled properly by some logging infrastructure I have to use.

一种解决方法是将\n替换为另一个字符,例如_newline_

A workaround is to replace the \n with another character, e.g. _newline_

这可以通过配置模式在logback中完成:

This can be done in logback by configuring the pattern:

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
  <encoder>
    <!-- Standard pattern -->
    <!-- <pattern>%coloredLevel - %logger - %message%n%xException{15}</pattern> -->
    <!-- With newlines removed -->
    <pattern>%coloredLevel - %logger - %replace(%message){'\n', '_newline_'}_newline_%replace(%xException){'\n', '_newline_'}%nopex%n</pattern>
  </encoder>
</appender>

但是,这在没有异常的情况下在日志行中添加了多余的_newline_. (并且在堆栈跟踪中添加了额外的换行符,但这不是一个大问题)

However, this adds a superfluous _newline_ in logslines when there isn't an exception. (and adds an extra newline to stack traces, but this isn't a large problem)

是否有办法仅在消息与是否有异常之间输出_newline_?

Is there a way to only output the _newline_ in between the message and the if there is an exception?

推荐答案

找到了一些简单的解决方案,以有条件地在日志消息和异常消息之间输出换行符.看来,无论您打印多少行堆栈跟踪信息,异常消息总是跟随换行符.您可以做的是打印一条带有0行堆栈跟踪(%ex{0})的异常,然后从左侧截断所有字符,直到长度不超过1(%.1ex{0}).如果有异常,这将有效地留下\n,并在没有异常的情况下产生空字符串.

Found a bit easier solution to conditionally output newline between log message and exception message. It seems that exception message is always followed by newline, no matter how many lines of stack trace you print. What could you do is print an exception with 0 lines of stack trace (%ex{0}), and then truncate from left all the characters until the length is no more than 1 (%.1ex{0}). This effectively leaves a \n if there's an exception and produces empty string in case there's no exception.

所以这个:

// logger.error("Log message", new RuntimeException("Exception message", new RuntimeException("Cause")));
// logger.error("Log message without exception");

<pattern>%-5p %m%.1ex{0}%ex%nopex%n</pattern>

结果:

ERROR Log message
java.lang.RuntimeException: Exception message
    [ redacted ]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.RuntimeException: Cause
    ... 10 common frames omitted

ERROR Log message without exception

现在很有趣.还记得例外情况之后总是换行符的事实吗?这会使带有异常的日志事件后跟空行(一个换行符来自异常,一个换行符来自模式),除非您知道一种技巧,仅在没有异常的情况下才打印某些内容...

Now the fun thing. Remember the fact that exceptions are always followed by newline? This makes the log events with exceptions to be followed by empty line (one newline from exception and one from the pattern), unless you know a trick how to print something only when there are no exceptions...

P.S.您也可以将所有内容包装在replace(){}中以换行:

P.S. you can wrap everything in replace(){} to escape newlines, too:

<pattern>%-5p %replace(%m%.1ex{0}%ex){'[\r\n]+', 'LF'}%nopex%n</pattern>

这篇关于如果登录中有异常,请添加一些要记录的内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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