javalogstashlogbacklogstash-logback-encoder

How can I combine PrettyPrintingJsonGeneratorDecorator and MaskingJsonGeneratorDecorator together?


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

<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

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

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

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


Solution

  • Use a CompositeJsonGeneratorDecorator, like this:

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