生成结构化日志Java

发布于 2025-02-09 09:31:36 字数 300 浏览 2 评论 0原文

我是新来的Java。

我想问一下如何使用Java这样的bellow产生日志。

{"log_level":"INFO","timestamp":"2021-12-23T08:39:48.444706Z",
"event_type":"abc_c","capture_id":"400011","cap_pid":"00053716",
"deb":"0100","no_id":"0011",
"msg_id":"d8c5derbn5-63cb-11ec-8980-0242ac110004","module":"S_TH}

I'm new with JAVA.

I want to ask how can I generate logs like this bellow using java.

{"log_level":"INFO","timestamp":"2021-12-23T08:39:48.444706Z",
"event_type":"abc_c","capture_id":"400011","cap_pid":"00053716",
"deb":"0100","no_id":"0011",
"msg_id":"d8c5derbn5-63cb-11ec-8980-0242ac110004","module":"S_TH}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

几味少女 2025-02-16 09:31:36

假设您想要日志文件中的JSON
(您的示例不是有效的JSON),
您可以使用 slf4j-json-logger

a href =“ https://www.baeldung.com/java-log-json-unput” rel =“ nofollow noreferrer”> baeldung文章讨论了更多的JSON记录解决方案。

Assuming that you want JSON in the log file
(your example is not valid JSON),
you can use slf4j-json-logger

Here is a Baeldung article that discusses more JSON logging solutions.

烦人精 2025-02-16 09:31:36

如果您使用的是slf4j和logback,则可以使用ch.qos.logback.contrib.json.jsonlaylayout,来自 https://github.com/qos-ch/logback-contrib/wiki/json

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
        </layout>
</appender>

或者,如果您的目标是Grafana Loki,则可以直接通过 https:https:// loki4j。 github.io/loki-logback-appender/

对于log4j2,有内置的jsonlayout:

<Appenders>
    <Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
       <JsonLayout complete="false" compact="false">
       </JsonLayout>
   </Console>
</Appenders>

但是,这仅包括默认属性,例如时间戳,loglevel,logger名称和消息。如果要添加自定义属性,则SLF4J具有映射的诊断上下文:

    MDC.put("transaction.id", tx.getTransactionId());
    MDC.put("transaction.owner", tx.getOwner());
    logger.info("my message");
    MDC.clear();

LogBack将自动将所有MDC键值添加为“ MDC”属性下的JSON映射。

如果您使用的是log4j2,则MDC称为threadContext。

    ThreadContext.put("myKey", "myValue");
    log.info("Here's a message!");
    ThreadContext.clear();

对于Log4J2,您需要将上下文键明确添加到JSONLAYOUT中。

<Appenders>
    <Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
       <JsonLayout complete="false" compact="false">
            <KeyValuePair key="myKey" />
       </JsonLayout>
   </Console>
</Appenders>value="${ctx:myKey}"/>

If you're using SLF4j and logback, you can use the ch.qos.logback.contrib.json.JsonLayout from https://github.com/qos-ch/logback-contrib/wiki/JSON.

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
        </layout>
</appender>

Or if your target is Grafana Loki, you can send the logs directly via https://loki4j.github.io/loki-logback-appender/

For log4j2, there's the built-in JsonLayout:

<Appenders>
    <Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
       <JsonLayout complete="false" compact="false">
       </JsonLayout>
   </Console>
</Appenders>

However, this will only include default attributes like timestamp, loglevel, logger name, and message. If you want to add custom attributes, SLF4J has an Mapped Diagnostic Context for that:

    MDC.put("transaction.id", tx.getTransactionId());
    MDC.put("transaction.owner", tx.getOwner());
    logger.info("my message");
    MDC.clear();

Logback will automatically add all MDC key-values as a JSON map under the "mdc" attribute.

If you're using log4j2, the MDC is called ThreadContext.

    ThreadContext.put("myKey", "myValue");
    log.info("Here's a message!");
    ThreadContext.clear();

For log4j2, you need to explicitly add the context keys to the JsonLayout.

<Appenders>
    <Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
       <JsonLayout complete="false" compact="false">
            <KeyValuePair key="myKey" />
       </JsonLayout>
   </Console>
</Appenders>value="${ctx:myKey}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文