如何使用SLF4J使用占位符记录异常和消息 [英] How to log exception and message with placeholders with SLF4J

查看:331
本文介绍了如何使用SLF4J使用占位符记录异常和消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 SLF4J 记录错误消息和异常的正确方法是什么?

What's the correct approach to log both an error message and an exception using SLF4J?

我已经尝试过这样做,但是从不打印异常堆栈跟踪:

I've tried doing this but the exception stack trace is never printed:

logger.error(无法解析数据{}",inputMessage,e);

在这种情况下,我想用 inputMessage 填充 {} ,并注销异常stacktrace.

In this case I want to populate {} with the inputMessage as well as logging out the exception stacktrace.

我唯一看到的方法是这样做:

The only way I can see to do this would be to do this:

logger.error(无法解析数据" + inputMessage,e);

这不漂亮.

推荐答案

从SLF4J 1.6版开始,SLF4J将按您的预期解释最后一个参数,即作为例外.您必须使用旧版本的SLF4J API.

As of SLF4J version 1.6, SLF4J will interpret the last parameter as you intended, i.e. as an exception. You must be using an older version of SLF4J API.

这篇关于如何使用SLF4J使用占位符记录异常和消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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