英文:
Structured logging where logger argument not wanted in message
问题
// 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"))
英文:
I'm using structured logging in a Spring Boot app using logstash and sometimes I want to include key values in the log that I don't want to be used in the message of the log. Is there a StructuredArgument or similar that allows for this?
An example of what I am doing currently is something like this:
// 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"))
Everything works as I intended, by which I mean the log includes both key value pairs and the message only includes the first value in place of the placeholder. The issue is that I get a warning from the compiler which is flagged by intellij (PlaceholderCountMatchesArgumentCount) about the second structured argument and I would like to avoid this without resorting to suppressing/ignoring it
答案1
得分: 3
你可以在记录日志信息之前使用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 2.0.0-alpha1版本,但它仍然处于alpha版本,我还没有使用过。但它的API应该已经准备好支持在今天的分布式日志管理系统中至关重要的键值。
英文:
You can use Markers and pass it before your logging message - more details on github.
logger.info(append("key2", "only json"),
"My message with one arg {}",
v("key1":"arg in msg and json"));
I personally don't like this because markers have different purpose, so if structured argument works for you, just ignore warning in IDE.
Anyway, all this json/structured implementations are workarounds for SLF4J 1.*, which has not built for that. There was SLF4J 2.0.0-alpha1 release almost a yeah ago, but it is still in alpha and I haven't used it. But it's API should be ready for key-values that are crusial in nowadays distributed log management systems.
答案2
得分: 0
你可以将日志消息设置为一个常量字符串,这样代码质量检查就不会发出警告。
英文:
You can make the log message as a constant String, then the code quality checks will not warn this
答案3
得分: 0
你可以使结构化参数在格式化的消息中打印出空内容:
- (1) 在消息中包含第二个占位符
{}
- (2) 使用
keyValue()
替代kv()
- (3) 提供可选的 messageFormatPattern 参数(JavaDoc)设置为
""
调整你的示例:
log.info(
"My message with one arg {}{}", // 注意 (1)
v("key1", "我想在消息和 JSON 中都包含值的参数"),
keyValue("key2", "我只想在 JSON 中而不在消息中包含的参数", "")) // 注意 (2) + (3)
这将有效地将第二个占位符替换为一个空字符串。
英文:
You can make the structured argument print nothing into the formatted message:
- (1) include the second placeholder
{}
inside the message - (2) use
keyValue()
instead ofkv()
- (3) provide the optional messageFormatPattern parameter (JavaDoc) equal to
""
Adjusting your example:
log.info(
"My message with one arg {}{}", //note (1)
v("key1":"arg I want to include value in place of placeholder in message and in json as key/value"),
keyValue("key2", "arg I only want in the json and not in the message", "")) //note (2) + (3)
This will effectively replace the second placeholder with an empty string.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论