我想找到一个 API,它允许我在我的应用程序的所有关键日志点中提供一组特定的规范信息。更具体地说,它将是一条多行消息,其中包含以下信息(除了基本信息):
- 日志记录类别
- 简短描述 标题
- 描述
- 我的应用程序将采取的响应措施(损害控制)
- 详细信息(异常信息等)
例如,使用单个多行日志查找,如下所示:
2017-11-10 14:26:59,156 [main] WARN o.s.t.c.s.ExampleClass:
Caption: Unconformable data
Description: The data provided from the X datasource in order to perform Y operation could not be translated
Response Action: Application will discard unusable data into this component's DLQ
Details: The data string "x" was not of expected Integer type
<Some stacktrace>....
这是一个冗长的语句,它将非常详细地说明发生了什么、发生在哪里以及应用程序正在做什么以响应异常事件。
我能找到的最接近的是 JBoss 日志记录 API 和我在ActiveMQ Artemis 源代码中找到的一些代码示例。消息格式声明可以在单个文件中定义,如下所示:
@LogMessage(level = Logger.Level.WARN)
@Message(id = 202008, value = "Failed to check Address list {0}.",
format = Message.Format.MESSAGE_FORMAT)
void failedToParseAddressList(@Cause Exception e, String addressList);
并且可以通过以下方式在他们的代码中记录一条带有此消息的行:
ActiveMQUtilLogger.LOGGER.failedToParseAddressList(e, addressList);
这是我能找到的最接近我正在寻找的东西。很酷。但是,我没有使用 JBoss(也不想锁定该 API)。
我可以使用 LOG4J,它有一个StructuredDataMessage 和 Structured Data Lookup,可以在布局中使用,默认情况下我会使用它;我的代码可以委托给某个StructuredDataMessage
工厂来解决这个问题。但是,它比使用 JBoss API 之类的东西要笨重一些。
有没有人对这个问题有任何建议——无论是另一个 API、代码模式还是一个漂亮的技巧?