我已经用log4j2.xml
文件成功配置了Log4J 2,并且我通过Property
成功地在文件中设置了JNDI variable lookup的值。
但是,如果JNDI变量不存在,我想为Property
提供默认值。
这可能吗?
试试这个:
<Root level="${jndi:yourJndiVariableName:-DEFAULT}">
通常所有Log4j2查找都遵循以下模式:${type:key:-defaultValue}
。
是的:这可以通过使用默认属性映射来完成:
<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,这也适用于其他属性来源(例如环境变量,系统属性等)。