4

Is there a way to reuse/parameterize an encoder configuration in logback xml configuration file? I have read that "Each layout/encoder is associated with one and only one appender, referred to as the owning appender." But instead of using one encoder for all appenders, I just wonder if there is a hack to deduplicate the code. I have the following encoder used in multiple if/else condition.

<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
    <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
        <if condition='...'>
            <then>
            ...
            </then>
            <else>
                ...
            </else>
        </if>
        ...
    </layout>
</encoder>

Because the complete encoder configuration contains 30 lines and is a big duplication in the config, is there a way to define a variable for this so it gets reused? I tried putting this in a separate file called encoder.xml, and use include as the following in appender:

<springProperty scope="context" name="encoder" source="logback.encoder" defaultValue="encoder.xml"/>
<appender name="json-stdout" class="ch.qos.logback.core.ConsoleAppender">
    <include resource="${encoder}"/>
</appender> 

But it gives the error no applicable action for [include], current ElementPath is [[configuration][appender][include]].

DylanS
  • 41
  • 4

0 Answers0