覆盖 Logback 错误输出
在我的自定义异常类中,我重写了 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一种快速而肮脏的方法是这样的:
logger.warn("your_message_here \n{}", err.toString());
更好的方法是编写自己的自定义转换说明符(请参阅 logback 手册,以便从应用程序代码中抽象出自定义错误处理,并且您可以通过 logback.xml 中的自定义转换字来配置它的使用,
这是一个简单的示例,可以完成您所要做的事情:
然后在 logback.xml 中,您需要像这样引用它:
然后在编码器模式中使用 %myCon,如下所示:
请注意,添加 %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:
Then in your logback.xml you'll need to reference it like this:
And then use %myCon in an encoder pattern like this:
Note that adding %nopex stops the usual exception handling
我遇到了同样的问题。Logback.error调用异常的getMessage方法。
重写该方法是另一种方法。
I faced the same problem.Logback.error calls the getMessage method of a exception.
Overriding the method is another way.