Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logback: test if property is defined

In a Spring Boot / Logback project, I want to check if a system property is set to use a given appender or not. My code is:

<property name="MY_PROPERTY" value="${MY_PROPERTY:-}" />
....
<logger name="com.my.project" level="INFO">
    <if condition='property("MY_PROPERTY").equalsIgnoreCase("MY_PROPERTY_IS_UNDEFINED")'>
        <then>
            <appender-ref ref="STDOUT" />
        </then>
        <else>
            <appender-ref ref="APPENDER_WITH_ MY_PROPERTY" />
        </else>
    </if>
</logger>

The code above (seems to) works, but this is not nice. The property() method append _IS_UNDEFINED to the variable name and I use it...

I can't find any reliable documentation about it, is there a better way?

like image 632
Rolintocour Avatar asked Sep 02 '25 05:09

Rolintocour


1 Answers

Late to the party, but hey.

The logback manual (http://logback.qos.ch/manual/configuration.html) mentions the isDefined method:

The isDefined() method can be used to check whether a property is defined. For example, to check whether the property "k" is defined you would write isDefined("k") Similarly, if you need to check whether a property is null, the isNull() method is provided. Example: isNull("k").

like image 150
Saucistophe Avatar answered Sep 05 '25 01:09

Saucistophe