JSON布局与Logback Java中的模式。

huangapple go评论92阅读模式
英文:

JSON Layout with Pattern in Logback Java

问题

我在我的Spring Boot应用程序中使用logback进行日志记录,并使用以下模式:

"%d [%thread] %-5p [%c] [%F:%L] [trace=%X{X-B3-TraceId:-},span=%X{X-B3-SpanId:-}]  - %msg%n"

现在我想要切换到JSON布局以进行日志记录。但是我没有找到一种将该模式应用于我的日志的方法,因此会丢失上述许多信息。

<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
        <timestampFormat>yyyy-MM-dd'T'HH:mm:ss.SSSX</timestampFormat>
        <timestampFormatTimezoneId>Etc/UTC</timestampFormatTimezoneId>
        <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
            <prettyPrint>true</prettyPrint>
        </jsonFormatter>
    </layout>
</appender>

是否有其他方法可以实现相同的效果呢?

英文:

I am using logback for logging in my spring boot application and using the pattern as per:

&quot;%d [%thread] %-5p [%c] [%F:%L] [trace=%X{X-B3-TraceId:-},span=%X{X-B3-SpanId:-}]  - %msg%n&quot;

Now I want to move to the JSON layout for my logs. But I don't see a way to apply the pattern to my logs as a result many of the above information is lost.

&lt;appender name=&quot;stdout&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
            &lt;layout class=&quot;ch.qos.logback.contrib.json.classic.JsonLayout&quot;&gt;
                &lt;timestampFormat&gt;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSX&lt;/timestampFormat&gt;
                &lt;timestampFormatTimezoneId&gt;Etc/UTC&lt;/timestampFormatTimezoneId&gt;
                &lt;jsonFormatter class=&quot;ch.qos.logback.contrib.jackson.JacksonJsonFormatter&quot;&gt;
                    &lt;prettyPrint&gt;true&lt;/prettyPrint&gt;
                &lt;/jsonFormatter&gt;
            &lt;/layout&gt;
    &lt;/appender&gt;

Any alternative way to achieve the same?

答案1

得分: 7

我在实现`JsonLayout`后遇到了相同的问题,在我的`logback-spring.xml`中,我意识到我不能使用自定义模式。

在经过大量搜索和浪费了几个小时后,我发现了另一种类型的logAppender,它允许指定模式并以Json格式打印日志。

您需要使用`net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder`编码器,而不是使用`JsonLayout`。

示例如下:

<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
   <providers>
     <timestamp>
       <fieldName>timestamp</fieldName>
       <pattern>yyyy-MM-dd' 'HH:mm:ss.SSS</pattern>
     </timestamp>
     <pattern>您所需的模式</pattern>
   </providers>
</encoder>

编辑:
有关详细文档,您还可以查看这个GitHub链接:


<details>
<summary>英文:</summary>

I have came across the same problem after implementing the `JsonLayout` in my `logback-spring.xml` I realized I cannot put in my own custom pattern.

Well after googling a lot and wasting few hours I came across 1 more kind of logAppender which allows pattern and also print logs in Json format.

You need to use `net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder` encoder instead of `JsonLayout`

The sample implementation can be as follows:

<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<fieldName>timestamp</fieldName>
<pattern>yyyy-MM-dd' 'HH:mm:ss.SSS</pattern>
</timestamp>
<pattern> your desired pattern </pattern>
</providers>
</encoder>


Edit:
For in-details documentation you can also check the this [github link:][1] 


  [1]: https://github.com/logstash/logstash-logback-encoder#providers-for-loggingevents

</details>



# 答案2
**得分**: 0

你在lagback.xml中&lt;appender&gt;和&lt;layout&gt;之间丢失了&lt;encoder&gt;。





yyyy-MM-dd'T'HH:mm:ss.SSSZZ
Etc/UTC

            <jsonFormatter class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                <prettyPrint>true</prettyPrint>
            </jsonFormatter>
        </layout>
    </encoder>
</appender>

<root level="INFO">
    <appender-ref ref="stdout"/>
</root>


```

英文:

you lose the <encoder> between <appender> and <layout> in you lagback.xml

&lt;configuration debug=&quot;true&quot;&gt;
&lt;appender name=&quot;stdout&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
    ***&lt;encoder class=&quot;ch.qos.logback.core.encoder.LayoutWrappingEncoder&quot;&gt;***
        &lt;layout class=&quot;ch.qos.logback.contrib.json.classic.JsonLayout&quot;&gt;
            &lt;timestampFormat&gt;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSSZZ&lt;/timestampFormat&gt;
            &lt;timestampFormatTimezoneId&gt;Etc/UTC&lt;/timestampFormatTimezoneId&gt;

            &lt;jsonFormatter class=&quot;ch.qos.logback.contrib.jackson.JacksonJsonFormatter&quot;&gt;
                &lt;prettyPrint&gt;true&lt;/prettyPrint&gt;
            &lt;/jsonFormatter&gt;
        &lt;/layout&gt;
    &lt;/encoder&gt;
&lt;/appender&gt;

&lt;root level=&quot;INFO&quot;&gt;
    &lt;appender-ref ref=&quot;stdout&quot;/&gt;
&lt;/root&gt;

</configuration>

答案3

得分: 0

从logback 1.3.8/1.4.8版本开始,内置支持JSON,因此您只需更改配置即可。

因此,您的配置文件 logback-spring.xml 将如下所示:

<configuration>
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.JsonEncoder"/>
    </appender>

    <root>
        <level value="DEBUG"/>
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>

然后,您将在控制台上获得JSON输出。

英文:

As of logback 1.3.8/1.4.8 there is built in support for JSON https://logback.qos.ch/manual/encoders.html#JsonEncoder so you can just change the config

So you config file logback-spring.xml would look like

&lt;configuration&gt;
    &lt;appender name=&quot;CONSOLE&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
        &lt;encoder class=&quot;ch.qos.logback.classic.encoder.JsonEncoder&quot;/&gt;
    &lt;/appender&gt;

    &lt;root&gt;
        &lt;level value=&quot;DEBUG&quot;/&gt;
        &lt;appender-ref ref=&quot;CONSOLE&quot;/&gt;
    &lt;/root&gt;
&lt;/configuration&gt;

Then you will get JSON output to the console

huangapple
  • 本文由 发表于 2020年9月11日 14:33:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/63841905.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定