用于漂亮打印json的logback格式化程序

7kjnsjlb  于 2021-08-20  发布在  Java
关注(0)|答案(0)|浏览(594)

目标:我希望我的应用程序代码将不漂亮的json记录为字符串。这是需要保持它作为一个事件的splunk。但是,在登录本地时,如果需要,可以使用不同的logback.xml,我希望它能够打印出来。
试过以下几点:我已经试过了 logstashJacksonJsonFormatter . 两人似乎都没有做好印刷工作。以下是配置和示例输出:

<encoder class="net.logstash.logch.qos.logback.contrib.jsonback.encoder.LogstashEncoder">
            <fieldNames>
                <timestamp>[ignore]</timestamp>
                <level>[ignore]</level>
                <levelValue>[ignore]</levelValue>
                <thread>[ignore]</thread>
                <logger>[ignore]</logger>
                <version>[ignore]</version>
                <message/>
            </fieldNames>
            <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                <prettyPrint>true</prettyPrint>
            </jsonFormatter>
            &lt;!&ndash;<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
                <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                    <prettyPrint>true</prettyPrint>
                </jsonFormatter>
                <includeContextName>false</includeContextName>
                <appendLineSeparator>false</appendLineSeparator>
            </layout>&ndash;&gt;
        </encoder>

------
Results:
{"message":"{\"interface\":\"dynamodb client\",\"action\":\"test dynamodb connection\",\"status\":\"ok\",\"table-status\":\"ACTIVE\"}"}

请求:有人能建议我在这里所做的更正吗?或者是一个好的库,它提供了编码器和格式化程序,可以在登录之前漂亮地打印json字符串?
我有密码
代码: logger.info(statusJsonStr); logback.xml:

<encoder>
  <pattern>%message%n</pattern>
</encoder>

控制台日志如下所示:

{"interface":"dynamodb client","action":"test dynamodb connection","status":"ok","table-status":"ACTIVE"}

类路径中的库

circe                   = "0.13.0" //for circe-core, circe-parser, circe-jawn
logbackClassic          = "1.2.3" //for  logback-classic & logback-core

    "net.logstash.logback"       % "logstash-logback-encoder" % "4.11",
    "ch.qos.logback.contrib"     % "logback-json-core"        % "0.1.2",
    "ch.qos.logback.contrib"     % "logback-json-classic"     % "0.1.2",
    "ch.qos.logback.contrib"     % "logback-jackson"          % "0.1.2",
    "com.fasterxml.jackson.core" % "jackson-core"             % "2.5.2",
    "com.fasterxml.jackson.core" % "jackson-databind"         % "2.5.2",
    "com.fasterxml.jackson.core" % "jackson-annotations"      % "2.5.2",

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题