使用Log4J 2查找JNDI变量查找时如何设置默认值?

时间:2017-07-26 18:55:32

标签: java logging jndi log4j2

我已经使用log4j2.xml文件成功配置了Log4J 2,并且我通过以下方式成功设置了文件Property的值 JNDI variable lookup

但是,如果JNDI变量不存在,我想为Property提供默认值。

这可能吗?

2 个答案:

答案 0 :(得分:1)

试试这个:

<Root level="${jndi:yourJndiVariableName:-DEFAULT}">

通常所有Log4j2查找都遵循以下模式:${type:key:-defaultValue}

答案 1 :(得分:0)

是:可以使用默认属性映射来完成此操作:

<Configuration status="DEBUG" name="Example">
    <Properties>
        <Property name="yourJndiVariableName">
            the value used if the JNDI variable cannot be found
        </Property>
    </Properties>

    ... more configuration ...

    <Loggers>
        <Root level="${jndi:yourJndiVariableName}">
            <AppenderRef ref="console"/>
        </Root>
    </Loggers>

    ... more configuration ...
</Configuration>

According to the Log4J 2 configuration documentation for property substitution,这也适用于其他属性来源(例如环境变量,系统属性等)。