是否在单行上有异常的登录配置? [英] Logback configuration to have exceptions on a single line?

查看:31
本文介绍了是否在单行上有异常的登录配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的日志被提取、输送并合并到ElasticSearch中。多行事件很难跟踪和诊断。

与其使用收集器和正则表达式将异常行分组到单个记录中,是否可以使用logback configurationException堆栈跟踪放在单个行中?

推荐答案

您可以在logback.xml中为%ex符号声明转换规则,如下所示:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <conversionRule conversionWord="ex" converterClass="com.foo.CompressedStackTraceConverter" /> 

    ...

</configuration>

然后声明CompressedStackTraceConverter如下:

import ch.qos.logback.classic.pattern.ThrowableProxyConverter;
import ch.qos.logback.classic.spi.IThrowableProxy;

public class CompressedStackTraceConverter extends ThrowableProxyConverter {
    @Override
    protected String throwableProxyToString(IThrowableProxy tp) {
        String original = super.throwableProxyToString(tp);

        // replace the new line characters with something, 
        // use your own replacement value here
        return original.replaceAll("
", " ~~ ");
    }
}
只要您的日志记录模式包含%ex符号,custom conversion specifier就会起作用。例如,模式如下:

<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
    <pattern>%d{yyyy-MM-dd HH:mm:ss}|%-5level|%logger{36}|%msg %ex %n</pattern>
</encoder>

如果您的模式包括%ex符号,则堆栈跟踪是%msg的一部分,在这种情况下,您可以这样声明转换规则...

<conversionRule conversionWord="msg" converterClass="com.foo.CompressedStackTraceConverter" /> 

.尽管这将对您的整个日志消息应用CompressedStackTraceConverter,而不仅仅是它的堆栈跟踪部分。

这篇关于是否在单行上有异常的登录配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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