1

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

vishal palla
  • 41
  • 1
  • 8
  • Where do you see in logback documentation that it allows expressions in properties? – aled May 10 '21 at 14:29

1 Answers1

1

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>
    ...
samabcde
  • 6,988
  • 2
  • 25
  • 41