覆盖 Logback 错误输出

发布于 2024-11-01 15:20:59 字数 1176 浏览 4 评论 0原文

在我的自定义异常类中,我重写了 toString() :(

@Override
public String toString() {
    final String msg = getLocalizedMessage();

    // base
    String str = getClass().getName() + ": [" + code + "]";

    // message
    if (msg != null)
        str += " " + msg;

    // extra
    if (extra != null) {
        str += '\n' + extra.toString();
    }

    return str;
}

是的,我知道我应该在那里使用 StringBuilder )

但是,当我记录这样的异常时(通过org.slf4j.Logger.warn(String msg, Throwable err)) 输出与普通异常相同:

webersg.util.service.ServiceError: null
    at webersg.util.service.ServiceTools.decodeException(ServiceTools.java:39) ~[bin/:na]
    at tr.silvercar.rummikub.robot.LobbyConnection.sendRequestTo(LobbyConnection.java:143) ~[bin/:na]
    at tr.silvercar.rummikub.robot.LobbyConnection.sendRequest(LobbyConnection.java:98) ~[bin/:na]
    at tr.silvercar.rummikub.robot.Robot.<init>(Robot.java:32) ~[bin/:na]
    at tr.silvercar.rummikub.robot.RobotController.start(RobotController.java:81) ~[bin/:na]
    at tr.silvercar.rummikub.robot.runners.LocalAltinRobot.main(LocalSilverRobot.java:132) [bin/:na]

如何才能显示额外的数据?我的日志实现是Logback。

In my custom exception class I've overridden toString():

@Override
public String toString() {
    final String msg = getLocalizedMessage();

    // base
    String str = getClass().getName() + ": [" + code + "]";

    // message
    if (msg != null)
        str += " " + msg;

    // extra
    if (extra != null) {
        str += '\n' + extra.toString();
    }

    return str;
}

(yes, I'm aware I should use StringBuilder there)

However, when I log such an exception (via org.slf4j.Logger.warn(String msg, Throwable err)) the output is as for vanilla exceptions:

webersg.util.service.ServiceError: null
    at webersg.util.service.ServiceTools.decodeException(ServiceTools.java:39) ~[bin/:na]
    at tr.silvercar.rummikub.robot.LobbyConnection.sendRequestTo(LobbyConnection.java:143) ~[bin/:na]
    at tr.silvercar.rummikub.robot.LobbyConnection.sendRequest(LobbyConnection.java:98) ~[bin/:na]
    at tr.silvercar.rummikub.robot.Robot.<init>(Robot.java:32) ~[bin/:na]
    at tr.silvercar.rummikub.robot.RobotController.start(RobotController.java:81) ~[bin/:na]
    at tr.silvercar.rummikub.robot.runners.LocalAltinRobot.main(LocalSilverRobot.java:132) [bin/:na]

How can I get my extra data to appear? My log implementation is Logback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

睡美人的小仙女 2024-11-08 15:20:59

一种快速而肮脏的方法是这样的:
logger.warn("your_message_here \n{}", err.toString());

更好的方法是编写自己的自定义转换说明符(请参阅 logback 手册,以便从应用程序代码中抽象出自定义错误处理,并且您可以通过 logback.xml 中的自定义转换字来配置它的使用,

这是一个简单的示例,可以完成您所要做的事情:

package com.example.logging;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.ThrowableProxy;

/**
 * Simple exception converter.
 *
 */
public class MyExceptionConverter extends ClassicConverter {
    /* (non-Javadoc)
     * @see ch.qos.logback.core.pattern.Converter#convert(java.lang.Object)
     */
    @Override
    public String convert(ILoggingEvent event) {
        IThrowableProxy itp = event.getThrowableProxy();
        if (itp instanceof ThrowableProxy) {
            ThrowableProxy tp = (ThrowableProxy)itp;
            return tp.getThrowable().toString();
        }

        return "";
    }
}

然后在 logback.xml 中,您需要像这样引用它:

  <conversionRule conversionWord="myCon"
              converterClass="com.example.logging.MyExceptionConverter" />

然后在编码器模式中使用 %myCon,如下所示:

<encoder>
    <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} %logger{0}: %myCon %nopex%n</pattern>
</encoder>

请注意,添加 %nopex 会停止通常的异常处理

A quick and dirty way is this:
logger.warn("your_message_here \n{}", err.toString());

A better way would be to write your own custom conversion specifier (see logback manual so that the custom error handling is abstracted from your application code, and you can configure it's use through a custom conversion word in your logback.xml.

Here's a simple example that will do what you're after:

package com.example.logging;

import ch.qos.logback.classic.pattern.ClassicConverter;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.IThrowableProxy;
import ch.qos.logback.classic.spi.ThrowableProxy;

/**
 * Simple exception converter.
 *
 */
public class MyExceptionConverter extends ClassicConverter {
    /* (non-Javadoc)
     * @see ch.qos.logback.core.pattern.Converter#convert(java.lang.Object)
     */
    @Override
    public String convert(ILoggingEvent event) {
        IThrowableProxy itp = event.getThrowableProxy();
        if (itp instanceof ThrowableProxy) {
            ThrowableProxy tp = (ThrowableProxy)itp;
            return tp.getThrowable().toString();
        }

        return "";
    }
}

Then in your logback.xml you'll need to reference it like this:

  <conversionRule conversionWord="myCon"
              converterClass="com.example.logging.MyExceptionConverter" />

And then use %myCon in an encoder pattern like this:

<encoder>
    <pattern>%d{dd MMM yyyy HH:mm:ss.SSS} %logger{0}: %myCon %nopex%n</pattern>
</encoder>

Note that adding %nopex stops the usual exception handling

狼性发作 2024-11-08 15:20:59

我遇到了同样的问题。Logback.error调用异常的getMessage方法。
重写该方法是另一种方法。

@Override
public String getMessage() {
    return toString();
}

I faced the same problem.Logback.error calls the getMessage method of a exception.
Overriding the method is another way.

@Override
public String getMessage() {
    return toString();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文