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

问题描述 投票:2回答:2

我已经用log4j2.xml文件成功配置了Log4J 2,并且我通过Property成功地在文件中设置了JNDI variable lookup的值。

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

这可能吗?

java logging jndi log4j2
2个回答
1
投票

试试这个:

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

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


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,这也适用于其他属性来源(例如环境变量,系统属性等)。

© www.soinside.com 2019 - 2024. All rights reserved.