0

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

   @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、代码模式还是一个漂亮的技巧?

4

1 回答 1

2

你没有说明你不使用 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

最后的一些想法:

我的代码可以委托给一些 StructuredDataMessage 工厂来解决这个问题

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

但是,它比使用 JBoss API 之类的东西要笨重一些。

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

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

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

于 2017-11-11T07:06:00.710 回答