简体   繁体   English

对从 logback.xml 中的 application.properties 读取的值进行条件检查

[英]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我正在读取包含来自 application.properties 的 UserService 值的 ${name},我需要在 logback.xml 中有条件地执行以下操作

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

But when I print myProp i am getting但是当我打印 myProp 我得到

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

But I need to print it either 1 or 2 based on condition.但我需要根据条件打印 1 或 2。 Or If My approach is wrong, how can I do the condtional checking in logback.xml或者如果我的方法是错误的,我该如何在 logback.xml 中进行条件检查

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.参考Unable to use Spring Property Placeholders in logback.xml ,我们利用logback-spring.Z0F635D0E0F3874FFF8B581C132E6C77A中的springProperty标签读取属性值。

<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然后我们可以按照配置文件的条件处理来为项目添加janino依赖

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

Finally add the conditional property to logback-spring.xml最后将条件属性添加到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>
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM