如何将 PrettyPrintingJsonGeneratorDecorator 和 MaskingJsonGeneratorDecorator 结合在一起?

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

How can I combine PrettyPrintingJsonGeneratorDecorator and MaskingJsonGeneratorDecorator together?

问题

我已尝试配置logstash-logback-encoder版本6.4以打印漂亮的JSON格式,如下所示:

<appender name="consoleAsJSON" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        <includeCallerData>true</includeCallerData>
        <jsonGeneratorDecorator class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
    </encoder>
</appender>

输出符合预期,为漂亮的打印。然后,我尝试配置掩码,如下所示:

<appender name="consoleAsJSON" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        <includeCallerData>true</includeCallerData>
        <jsonGeneratorDecorator class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
            <defaultMask>XXXX</defaultMask>
            <path>password</path>
        </jsonGeneratorDecorator>
    </encoder>
</appender>

输出符合预期,为标记的格式。然而,当我将这两者结合在一起时,它只能执行其中一个操作,要么是漂亮的打印要么是标记的

<appender name="consoleAsJSON" class="ch.qos.logback.core.ConsoleAppender">
    <encoder class="net.logstash.logback.encoder.LogstashEncoder">
        <includeCallerData>true</includeCallerData>
        <jsonGeneratorDecorator class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
        <jsonGeneratorDecorator class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
            <defaultMask>XXXX</defaultMask>
            <path>password</path>
        </jsonGeneratorDecorator>
    </encoder>
</appender>

这只会执行标记的操作,而不会进行漂亮的打印。同样地,如果将这两个装饰器的顺序颠倒,将只执行漂亮的打印而不进行标记的操作。

您如何才能将PrettyPrintingJsonGeneratorDecoratorMaskingJsonGeneratorDecorator结合在一起?能否请您提供建议?

英文:

I've tried to config the logstash-logback-encoder version 6.4 to print the pretty print JSON as

<!-- language: xml -->
<appender name="consoleAsJSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeCallerData>true</includeCallerData>
<jsonGeneratorDecorator class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
</encoder>
</appender>

The output is pretty print as expected. Then I step forward to config with masking as

<!-- language: xml -->
<appender name="consoleAsJSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeCallerData>true</includeCallerData>
<jsonGeneratorDecorator class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
<defaultMask>XXXX</defaultMask>
<path>password</path>
</jsonGeneratorDecorator>
</encoder>
</appender>

The output is marked as expected, too.

Anyhow when I combine these two together, it only does one thing, either pretty print or masked.

<!-- language: xml -->
<appender name="consoleAsJSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeCallerData>true</includeCallerData>
<jsonGeneratorDecorator class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
<jsonGeneratorDecorator class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
<defaultMask>XXXX</defaultMask>
<path>password</path>
</jsonGeneratorDecorator>
</encoder>
</appender>

It only does the masked, but not pretty print.

<!-- language: xml -->
<appender name="consoleAsJSON" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<includeCallerData>true</includeCallerData>
<jsonGeneratorDecorator class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
<defaultMask>XXXX</defaultMask>
<path>password</path>
</jsonGeneratorDecorator>
<jsonGeneratorDecorator class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
</encoder>
</appender>

It only does the pretty print, but not masked.

How can I combine PrettyPrintingJsonGeneratorDecorator and MaskingJsonGeneratorDecorator together? Could you please help to advise?

答案1

得分: 5

使用 CompositeJsonGeneratorDecorator,如下所示:

&lt;appender name=&quot;consoleAsJSON&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
    &lt;encoder class=&quot;net.logstash.logback.encoder.LogstashEncoder&quot;&gt;
        &lt;includeCallerData&gt;true&lt;/includeCallerData&gt;
        &lt;jsonGeneratorDecorator class=&quot;net.logstash.logback.decorate.CompositeJsonGeneratorDecorator&quot;&gt;
            &lt;decorator class=&quot;net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator&quot;/&gt;
            &lt;decorator class=&quot;net.logstash.logback.mask.MaskingJsonGeneratorDecorator&quot;&gt;
                &lt;defaultMask&gt;XXXX&lt;/defaultMask&gt;
                &lt;path&gt;password&lt;/path&gt;
            &lt;/decorator&gt;
        &lt;/jsonGeneratorDecorator&gt;
    &lt;/encoder&gt;
&lt;/appender&gt;
英文:

Use a CompositeJsonGeneratorDecorator, like this:

&lt;appender name=&quot;consoleAsJSON&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
    &lt;encoder class=&quot;net.logstash.logback.encoder.LogstashEncoder&quot;&gt;
        &lt;includeCallerData&gt;true&lt;/includeCallerData&gt;
        &lt;jsonGeneratorDecorator class=&quot;net.logstash.logback.decorate.CompositeJsonGeneratorDecorator&quot;&gt;
            &lt;decorator class=&quot;net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator&quot;/&gt;
            &lt;decorator class=&quot;net.logstash.logback.mask.MaskingJsonGeneratorDecorator&quot;&gt;
                &lt;defaultMask&gt;XXXX&lt;/defaultMask&gt;
                &lt;path&gt;password&lt;/path&gt;
            &lt;/decorator&gt;
        &lt;/jsonGeneratorDecorator&gt;
    &lt;/encoder&gt;
&lt;/appender&gt;

huangapple
  • 本文由 发表于 2020年9月5日 20:07:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/63753713.html
匿名

发表评论

匿名网友

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

确定