问题描述
在我的自定义异常类中,我覆盖了toString()
:
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;
}
(是的,我知道我应该在那里使用StringBuilder
)
(yes, I'm aware I should use StringBuilder
there)
但是,当我记录这样的异常(通过org.slf4j.Logger.warn(String msg, Throwable err)
)时,输出与普通异常相同:
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]
如何显示我的额外数据?我的日志实现是Logback.
How can I get my extra data to appear? My log implementation is Logback.
推荐答案
一种快速而肮脏的方法是这样的:logger.warn("your_message_here \ n {}",err.toString());
A quick and dirty way is this:logger.warn("your_message_here \n{}", err.toString());
更好的方法是编写自己的自定义转换说明符(请参见 logback手册,以便从应用程序代码中抽象出自定义错误处理,并且您可以通过logback.xml中的自定义转换字来配置它的使用.
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 "";
}
}
然后在logback.xml中,您需要像这样引用它:
Then in your logback.xml you'll need to reference it like this:
<conversionRule conversionWord="myCon"
converterClass="com.example.logging.MyExceptionConverter" />
然后在像这样的编码器模式中使用%myCon:
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>
请注意,添加%nopex会停止通常的异常处理
Note that adding %nopex stops the usual exception handling
这篇关于覆盖Logback错误输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!