gpt4 book ai didi

用于详细(和多行)日志的 Java Logging API

转载 作者:行者123 更新时间:2023-12-02 02:30:17 24 4
gpt4 key购买 nike

我希望找到一个 API,它允许我在应用程序的所有关键日志点中提供特定的规范信息集。更具体地说,它将是一条多行消息,其中包含以下信息(除了基本信息之外):

  1. 日志类别
  2. 简短描述标题
  3. 描述
  4. 我的应用程序将采取的响应行动(损害控制)
  5. 详细信息(异常信息等)

例如,使用单个多行日志,如下所示:

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 source 中找到的一些代码的示例。 。消息格式声明可以在单个文件中定义,如下所示:

   @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);

人们会记录 line with this message在他们的代码中编写:

ActiveMQUtilLogger.LOGGER.failedToParseAddressList(e, addressList);

这是我能找到的最接近我正在寻找的内容。很酷。但是,我没有使用 JBoss(也不想锁定该 API)。

我可以使用 LOG4J,它有 StructuredDataMessage and Structured Data Lookup它可以在布局中使用,默认情况下我将使用它;我的代码可以委托(delegate)给一些 StructuredDataMessage 工厂来解决这个问题。然而,它比使用 JBoss API 之类的东西有点笨重。

有人对这个问题有任何建议吗——无论是另一个 API、代码模式还是巧妙的技巧?

最佳答案

您没有说明为什么您不使用 log4j2 的任何具体原因,因此我建议您采用该路线。正如您所指出的,它提供了 StructuredDataMessage ,您可以使用它来满足您的需求。尽管在这种情况下,我建议使用 MapMessage 因为您的示例不包含 idtype 等内置于 中的内容StructuredDataMessage 类。

这里有一些快速示例代码:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.MapMessage;

public class MapMessageExample {

private static final Logger log = LogManager.getLogger();

public static void main(String[] args){
log.info(buildMsg("My title", "This is the description",
"No response needed", "Some details here"));
}

// This could be moved into a factory class
public static MapMessage buildMsg(String title, String description,
String responseAction, String details)
{
MapMessage mapMsg = new MapMessage();
mapMsg.put("title", title);
mapMsg.put("desc", description);
mapMsg.put("response", responseAction);
mapMsg.put("details", details);
return mapMsg;
}
}

以及与之配套的 log4j2.xml 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%M] %-5level %logger{36}%n\tCaption: ${map:title}%n\tDescription: ${map:desc}%n\tResponse Action: ${map:response}%n\tDetails: ${map:details}%n" />
</Console>
</Appenders>

<Loggers>
<Root level="debug">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>

这是一些示例输出:

00:40:45.810 [main] INFO  example.MapMessageExample
Caption: My title
Description: This is the description
Response Action: No response needed
Details: Some details here

一些最后的想法:

my code could delegate to some StructuredDataMessage factory to solve this

我同意,工厂模式在这里是一个不错的选择。正如我在示例代码中评论的那样,您可以将 buildMsg 方法移至工厂类。

However, it's a little bit bulkier than using something like this JBoss API.

我真的不明白它有什么笨重的地方。如果您发现大多数时候 MapMessage 中只有一两个项目发生变化,您可以轻松编写类似于您提到的 API 的非常具体的方法。添加到上面的示例:

public static MapMessage reallySpecificLogMessage(String details){
return buildMsg("My specific message title", "My specific description",
"My specific response action", details);
}

您可能希望将常量分配给此方法中使用的字符串,但正如我所说,这只是一个简单的示例。

关于用于详细(和多行)日志的 Java Logging API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47230293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com