Logback-如何分别记录异常的简单名称

时间:2018-09-26 03:51:17

标签: java spring logging logback slf4j

有什么方法可以仅记录异常的简单名称,而无需从代码中显式检索它?

例如,通过致电

log.error(exception);

具有登录模式

%d{yyyy-MM-dd}|%-5level|%m%n

不仅仅是记录异常堆栈跟踪

2018-01-01|ERROR|
mainPackage.foo.bar.RocketExplosionException: Houston we have a problem
  at mainPackage.foo.bar.TestThrower.fire(TestThrower.java:22)
  at mainPackage.foo.bar.TestThrower.readyToLaunch(TestThrower.java:17)
  at mainPackage.ExceptionLauncher.main(ExceptionLauncher.java:38)

预计将记录一个单独的具有异常简单名称的列

2018-01-01|ERROR|RocketExplosionException|
mainPackage.foo.bar.RocketExplosionException: Houston we have a problem
  at mainPackage.foo.bar.TestThrower.fire(TestThrower.java:22)
  at mainPackage.foo.bar.TestThrower.readyToLaunch(TestThrower.java:17)
  at mainPackage.ExceptionLauncher.main(ExceptionLauncher.java:38)

2 个答案:

答案 0 :(得分:3)

您可以编写自己的custom conversion specifier

为此,您需要在logback.xml中为%exname符号声明一个转换规则,如下所示:

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

    ...

</configuration>

然后像这样声明ExceptionNameConverter

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

public class ExceptionNameConverter extends ThrowableProxyConverter {
    @Override
    protected String throwableProxyToString(IThrowableProxy tp) {
        return tp.getClassName();
    }
}

现在,使用此模式:

%d{yyyy-MM-dd}|%-5level|%exname|%m%n

以下日志语句:

logger.error("Boom!", new RuntimeException("ouch"));

会发出:

2018-09-26|ERROR|java.lang.RuntimeException|Boom!

答案 1 :(得分:0)

Andreas建议,支持异常名称记录的一种方法是使用自定义Layout而不是PatternLayout

示例布局:

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.ThrowableProxyUtil;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;

public class ErrorLoggingLayout extends LayoutBase<ILoggingEvent> {

    private static final char SEP = '|';

    @Override
    public String doLayout(final ILoggingEvent event) {
        StringBuilder sb = new StringBuilder(128);
        sb.append(event.getTimeStamp() - event.getLoggerContextVO().getBirthTime());
        sb.append(SEP);
        sb.append(event.getLevel());
        sb.append(SEP);
        sb.append(event.getThreadName());
        sb.append(SEP);
        StackTraceElement frame = event.getCallerData()[0];
        sb.append(extractSimpleName(frame.getClassName())).append('.').append(frame.getMethodName()).append(':').append(frame.getLineNumber());
        sb.append(SEP);
        IThrowableProxy throwableProxy = event.getThrowableProxy();
        if (throwableProxy != null) {
            String simpleName = extractSimpleName(throwableProxy.getClassName());
            sb.append(simpleName);
        }
        sb.append(SEP);
        sb.append(event.getFormattedMessage());
        sb.append(CoreConstants.LINE_SEPARATOR);
        if (throwableProxy != null) {
            sb.append(ThrowableProxyUtil.asString(throwableProxy));
        }
        return sb.toString();

    }

    @NotNull
    private String extractSimpleName(final String cName) {
        int index = cName.lastIndexOf('.');
        return cName.substring(index + 1);
    }
}

结果类似

7645|ERROR|Thread-Name-0|RocketConsole.report:22|RocketExplosionException|custom msg
mainPackage.foo.bar.RocketExplosionException: Houston we have a problem
  at mainPackage.foo.bar.TestThrower.fire(TestThrower.java:22)
  at mainPackage.foo.bar.TestThrower.readyToLaunch(TestThrower.java:17)
  at mainPackage.ExceptionLauncher.main(ExceptionLauncher.java:38)

但是,这种方法的不足之处在于输出格式可能与原始模式略有不同,并且需要花费更多的精力来格式化字段。