结构化日志记录,其中消息中不需要记录器参数

时间:2020-09-30 17:07:06

标签: java spring-boot logstash slf4j spring-logback

我正在使用Logstash在Spring Boot应用程序中使用结构化日志记录,有时我想在日志中包括不想在日志消息中使用的键值。是否存在允许这样做的StructuredArgument或类似的东西?

我目前正在做的一个例子是这样的:

// import lombok.extern.slf4j.Slf4j;
// import static net.logstash.logback.argument.StructuredArguments.kv;
// import static net.logstash.logback.argument.StructuredArguments.v;

log.info(
  "My message with one arg {}", 
  v("key1":"arg I want to include value in place of placeholder in message and in json as key/value"), 
  kv("key2", "arg I only want in the json and not in the message"))

一切正常,我的意思是说日志包含两个键值对,并且消息仅包含第一个值代替占位符。问题是我从编译器收到由intellij(PlaceholderCountMatchesArgumentCount)标记的关于第二个结构化参数的警告,我想避免这种情况而不必求助于抑制/忽略它

2 个答案:

答案 0 :(得分:0)

您可以使用Markers并在记录日志消息之前传递它-有关github的更多详细信息。

logger.info(append("key2", "only json"), 
               "My message with one arg {}", 
               v("key1":"arg in msg and json"));

我个人不喜欢这样,因为标记的用途不同,因此,如果结构化参数对您有用,则只需忽略IDE中的警告即可。

无论如何,所有这些json /结构化实现都是SLF4J 1. *的变通办法,而SLF4J 1. *尚未为此构建。差不多是在十年前,有SLF4J 2.0.0-alpha1版本发布了,但是它仍然是alpha版本,我还没有使用过。但是它的API应该已经准备好用于当今分布式日志管理系统中至关重要的键值。

答案 1 :(得分:0)

您可以将日志消息设为常量字符串,那么代码质量检查将不会对此发出警告