vishal palla
vishal palla

Reputation: 41

Conditional check for the value read from application.properties in logback.xml

I am reading the ${name} which contains value as UserService from application.properties, I need to conditionally perform these below operation in logback.xml

 <property name="myprop"
        value="#{'${name}' == 'UserService' ? '1' : '2' }"/>

But when I print myProp i am getting

#{'UserService' == 'UserService' ? '1' : '2' }

But I need to print it either 1 or 2 based on condition. Or If My approach is wrong, how can I do the condtional checking in logback.xml

Upvotes: 1

Views: 1949

Answers (1)

samabcde
samabcde

Reputation: 8114

Referring to Unable to use Spring Property Placeholders in logback.xml, we make use of springProperty tag in logback-spring.xml to read property value.

<configuration scan="true" scanPeriod="10 seconds">
    <springProperty scope="context" name="nameInLogBack" source="name"/>
    ...

Then we can follow Conditional processing of configuration files to add janino dependency to project

        <dependency>
            <groupId>org.codehaus.janino</groupId>
            <artifactId>janino</artifactId>
            <version>3.0.6</version>
        </dependency>

Finally add the conditional property to logback-spring.xml

<configuration scan="true" scanPeriod="10 seconds">
    ...
    <if condition='property("nameInLogBack").equals("UserService")'>
        <then>
            <property name="myprop" value="1"/>
        </then>
        <else>
            <property name="myprop" value="2"/>
        </else>
    </if>
    ...

Upvotes: 1

Related Questions