log4j2如何将文件中的属性变量读入log4j2 [英] log4j2 how to read property variable from file into log4j2

查看:2229
本文介绍了log4j2如何将文件中的属性变量读入log4j2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景:像往常一样,我们有各种生命周期,如dev。 stage,lt,prod所有这些都是在部署时从环境变量$ {lifecycle}中挑选出来的。
所以JNDI设置我们将$ {lifecycle} .properties存储为变量datasource.jndi.name = jdbc / xxx。由于其他bean也在使用此属性文件,因此验证了此类变量是否已加载& file在classpath中,但不知怎的,我无法在下面的JDBC Appender中的log4j2.xml中使用此变量。

Background: As usual we have various life cycles like dev. stage, lt, prod all these are picked at deploy time from environment variable ${lifecycle}. So JNDI setting we stores in ${lifecycle}.properties as variable datasource.jndi.name=jdbc/xxx. As other beans are also using this properties file, it is verified that such variable is loaded & file is in classpath, but somehow I am not able to consume this variable in log4j2.xml in below JDBC Appender.

<JDBC name="DBAppender" tableName="V1_QUERY_LOG" bufferSize="4" ignoreExceptions="false">
    <DataSource jndiName="${sys:datasource.jndi.name}" />
    <Column name="event_date" isUnicode="false" isEventTimestamp="true" />
    <Column name="log_level" isUnicode="false" pattern="%level" />
    <Column name="logger" isUnicode="false" pattern="%logger" />
    <Column name="message" isUnicode="false" pattern="%message" />
    <Column name="exception_msg" isUnicode="false" pattern="%ex{full}" />
</JDBC>

我也试过像$ {datasource.jndi.name}这样的选项,或者在那里我可以在任何方式适合该解决方案

I have tried some option like "${datasource.jndi.name}" too, or is there any way I can fit the solution in

<Properties>
 <Property name="datasource.jndi.name">get datasource.jndi.name from {lifecycle}.properties</property>
</Properties>


推荐答案

如果你没有使用java系统属性,而是环境变量,你不应该使用 $ {sys:variable} 前缀,而是 $ {env:variable} 前缀代替。另请参见 http://logging.apache.org/log4j/2 .x / manual / lookups.html#EnvironmentLookup

If you are not using java system properties, but environment variables, you should not use the ${sys:variable} prefix, but the ${env:variable} prefix instead. See also http://logging.apache.org/log4j/2.x/manual/lookups.html#EnvironmentLookup

这篇关于log4j2如何将文件中的属性变量读入log4j2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆