我已經用log4j2.xml
文件成功配置Log4J 2,並且我通過 JNDI variable lookup成功設置文件中Property
的值。如何在使用Log4J 2查找JNDI變量查找時設置默認值?
但是,如果JNDI變量不存在,我想爲Property
提供一個默認值。
這可能嗎?
我已經用log4j2.xml
文件成功配置Log4J 2,並且我通過 JNDI variable lookup成功設置文件中Property
的值。如何在使用Log4J 2查找JNDI變量查找時設置默認值?
但是,如果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,這也將爲其他財產來源(如環境變量,系統屬性等)工作。