0

我尝试将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>

输出pretty print符合预期。然后我向前一步配置掩码为

<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>

输出也marked符合预期。

无论如何,当我将这两者结合在一起时,它只做一件事,要么pretty print要么masked

<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>

它只做masked, 但是not pretty print

<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>

它只做pretty print, 但是not masked

我怎样才能结合PrettyPrintingJsonGeneratorDecorator在一起MaskingJsonGeneratorDecorator?你能帮忙建议吗?

4

1 回答 1

1

使用CompositeJsonGeneratorDecorator,如下所示:

<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.CompositeJsonGeneratorDecorator">
            <decorator class="net.logstash.logback.decorate.PrettyPrintingJsonGeneratorDecorator"/>
            <decorator class="net.logstash.logback.mask.MaskingJsonGeneratorDecorator">
                <defaultMask>XXXX</defaultMask>
                <path>password</path>
            </decorator>
        </jsonGeneratorDecorator>
    </encoder>
</appender>
于 2020-09-05T16:54:27.380 回答